我想就如何解決以下問題提出建議。不變類的設計模式建議
首先我有這些類是可變的。重要的類是包含作爲字節數組的圖像的ImageData。
public class RootData
{
public string Name { get; set; }
public string Description { get; set; }
public ImageData Image { get; set; }
public List<ChildData> Children { get; set; }
}
public class ChildData
{
public string Name { get; set; }
public ImageData Image { get; set; }
}
public class ImageData
{
public string Extension { get; set; }
public byte[] Data { get; set; }
}
類的第二個設置是這些,它們是不可變的。重要的類是ImageInfo,它包含磁盤上圖像的文件路徑。
public class RootInfo
{
private RootInfo()
{
}
public string Name
{
get { return this.name; }
}
public string Description
{
get { return this.description; }
}
public ImageInfo Image
{
get { return this.image; }
}
public IEnumerable<ChildInfo> Children
{
get { return this.children; }
}
public static RootInfo ToInfo(RootData rootData)
{
}
private readonly string name;
private readonly string description;
private readonly ImageInfo image;
private IEnumerable<ChildInfo> children;
}
public class ChildInfo
{
public string Name
{
get { return this.name; }
}
public ImageInfo Image
{
get { return this.image; }
}
private readonly string name;
private readonly ImageInfo image;
}
public class ImageInfo
{
public string Path
{
get { return this.path; }
}
private readonly string path;
}
現在,我想將RootData對象轉換爲RootInfo對象。我首先想到在RootInfo類中創建一個靜態方法(ToInfo),該類使用RootData對象創建一個RootInfo對象。但我不想讓ImageInfo類負責將圖像保存在磁盤上。
對於如何以乾淨的方式解決這個問題,您有什麼建議嗎?
注意:這只是一個例子,實際的類更復雜,但原理是相同的。
如何保存圖像?圖像何時保存? –
你是什麼意思關於「保存磁盤上的圖像」?在你的例子中沒有關於它的代碼。所以目前還不清楚有什麼問題。 – Win4ster