どんなもの?
どんなときに使う?
- 標準的なnewなどの方法で新しいオブジェクトを作ることによるコストが高い時。
たとえば何種類もの変数を持ち、その変数が様々な値を持つインスタンスを作成したいときなど
例
典型的な構成要素
- Prototype
- ConcretePrototype
- ProrotypeUser
*Prototypeを適用したクラスを利用し処理する
- Client
コード
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Prototype{
public:
virtual Prototype *clone(){};
};
class ConcretePrototype: public Prototype{
public:
ConcretePrototype(){}
Prototype *clone(){
return new ConcretePrototype();
}
};
int main(){
Prototype* obj1 = new ConcretePrototype();
Prototype* obj2 = obj1->clone();
}