1、類圖
例項類圖
2、建立專案
3、 新建週報類weeklylog:充當原型角色,clone()方法為轉殖方法,用於實現原型物件的轉殖,attachmentch充當成員類。
attachmentch**如下:
using system;
namespace prototypesample
[serializable]
class attachment
private string name;
public string name
get
set
public void download()
4、weeklylog類**如下:
using system;
using system.io;
using system.collections;
using system.runtime.serialization.formatters.binary;
using system.runtime.serialization;
namespace prototypesample
[serializable]
class weeklylog
private attachment attachment;
private string name;
private string date;
private string content;
public attachment attachment
get
set
public string name
get
set
public string date
get
set
public string content
get
set
//使用memberwiseclone()方法實現淺轉殖
public weeklylog clone()
return this.memberwiseclone() as weeklylog;
//使用序列化方式實現深轉殖
public weeklylog clone()
weeklylog clone = null;
//序列化
filestream fs = new filestream("temp.dat", filemode.create);
binaryformatter formatter = new binaryformatter();
try//對當前物件執行序列化操作到temp.dat
formatter.serialize(fs, this);
catch (serializationexception e)
console.writeline("failed to serialize. reason: " + e.message);
throw;
finally
fs.close();
//反序列化
filestream fs1 = new filestream("temp.dat", filemode.open);
binaryformatter formatter1 = new binaryformatter();
try//從流中反序列化到物件
clone = (weeklylog)formatter1.deserialize(fs1);
catch (serializationexception e)
console.writeline("failed to deserialize. reason: " + e.message);
throw;
finally
fs.close();
return clone;
5、 客戶端測試類program:
using system;
namespace prototypesample
class program
static void main(string args)
concreteprototypeb prototype, copy;
prototype = new concreteprototypeb();
= "sunny";
copy = (concreteprototypeb)prototype.clone();
= "tom";
console.writeline(prototype == copy);
== copy.gettype());
console.writeline(prototype.member == copy.member);
console.read();
weeklylog log_previous, log_new;
log_previous = new weeklylog();
attachment attachment = new attachment();
log_previous.attachment = attachment;
log_new = log_previous.clone();
console.writeline("週報是否相同?",(log_previous == log_new)?"是":"否");
console.writeline("附件是否相同?",(log_previous.attachment == log_new.attachment)?"是":"否");
console.read();
6、 編譯及執行程式,輸出如下結果:
7、 深轉殖解決方案:
1) 將週報類weeklylog和附件類attachment標記為可序列化(serializable)
[serializable]
class weeklylog
private attachment attachment;
[serializable]
class attachment
//使用序列化方式實現深轉殖
public weeklylog clone()
weeklylog clone = null;
filestream fs = new filestream("temp.dat", filemode.create);
binaryformatter formatter = new binaryformatter();
tryformatter.serialize(fs, this); //序列化
catch (serializationexception e)
console.writeline("failed to serialize. reason: " + e.message);
throw;
finally
fs.close();
filestream fs1 = new filestream("temp.dat", filemode.open);
binaryformatter formatter1 = new binaryformatter();
tryclone = (weeklylog)formatter1.deserialize(fs1); //反序列化
catch (serializationexception e)
console.writeline("failed to deserialize. reason: " + e.message);
throw;
finally
fs1.close();
return clone;
設計模式之原型模式
原型模式其實就是從乙個物件再建立乙個可定製的物件,而且不需要知道建立的細節。具體就是說通過複製或者轉殖乙個原型物件產生新的物件 轉殖物件 分為兩種 深複製或者淺複製,這兩種的主要區別是 在原型中如果有物件的引用,淺複製是將引用複製過來了,也就是淺複製和原型中的物件的引用相同,而深複製是將轉殖物件中引...
設計模式之原型模式
include include 此練習為原型模式方法的練習 原型模式揭示了在同一原型上可以通過複製使得 只需要例項化一次,再通過相應的類函式操作可以得到 同一原型下的不同的版本物件 此練習將考慮在實際開發中 比如 工作經歷也是作為乙個類被含在簡歷類中的情況 這情況將涉及到類的引用和深 淺複製 原型基...
設計模式之原型模式
原型模式的思想就是將乙個物件作為原型,對其進行複製 轉殖,產生乙個和原物件類似的新物件 複製過程可分為淺複製和深複製。淺複製是指直接呼叫父類 即object類 的clone方法 super.clone 該方法是本地 native 方法,呼叫該方法clone出來的新物件,基本資料型別的屬性變數是新建立...