原型模式 Prototype)

2021-10-06 22:55:03 字數 840 閱讀 6330

原型模式:使用原型例項指定建立物件的種類,並通過複製這個原型建立新的物件

objective-c 實現原型模式應用場景:

cocoa touch框架為object的派生類提供了實現深複製的協議

nsobject的子類需要實現nscopying協議及方法 - (id)copywithzone:(nullable nszone *)zone

否則傳送copy訊息引發異常。

如果構造乙個複雜物件,這個物件不容易建立,這個物件需要複雜的步驟,這個時候考慮使用原型模式。

#import #import class iprototype;

class prototyeroot1:public iprototype

prototyeroot1(int age,std::string name,std::string otherthing)

iprototype* clone()

void print()

};class prototyeroot2:public iprototype

prototyeroot2(int age,std::string name,std::string address,int telphone,std::string otherthing)

iprototype* clone()

void print()

};//client 充當客戶端

原型模式(ProtoType)

1.目的 當需要從乙個物件copy出另外乙個物件時,可以使用原型模式,本質上是呼叫的copy建構函式 與copy建構函式的區別,可能就是不需要顯示的生成物件,而是通過函式呼叫 2.prototype.h ifndef prototype h define prototype h include in...

Prototype 原型模式

prototype 原型模式 所謂原型模式,就是可以根據乙個原型,快速建立出其拷貝。如 int a 1 int b new int a a cout a endl cout b endl 輸出結果 2 1 上面就使用了原型模式。根據原型a,快速建立出了a的拷貝b。同時,b儲存了a當時的狀態。當a之後...

原型模式 Prototype

用原型例項指定建立物件的種類,並且通過拷貝這些原型建立新的物件。原型模式是一種建立型設計模式,它允許乙個物件再建立另外乙個可定製的物件,根本無需知道任何如何建立的細節,工作原理是通過將乙個原型物件傳給那個要發動建立的物件,這個要發動的物件通過請求原型物件拷貝它們自己來實施建立。現實很殘酷,都第9章了...