Prototype

#include <iostream>
#include <string>
 
using namespace std;
 
class Prototype {
public:
	virtual Prototype* CreateClone() = 0;
	virtual void SetStatus(int status) {}
	virtual int GetStatus() {}
};
 
 
class Implement : public Prototype {
public:
	Implement() {
		m_status = 0;
	}
	Implement(const Implement& imp) {
		m_status = imp.m_status;
	}
	virtual Prototype* CreateClone() {
		return new Implement(*this);
	}
	virtual void SetStatus(int status) {
		m_status = status;
	}
	virtual int GetStatus() {
		return m_status;
	}
private:
	int m_status;
};
 
 
int main() {
	Prototype *obj = new Implement;
	obj->SetStatus(123);
 
	Prototype *clone = obj->CreateClone();
	cout << clone->GetStatus() << endl;
 
	// delete はどうするか。。
	// obj はここで new してるし。
	return 0;
}
 



参考サイト

デザインパターンを“喩え話”で分かり易く理解する
http://www.netlaputa.ne.jp/~hijk/study/oo/designpattern.html



デザインパターンの骸骨たち
http://www002.upp.so-net.ne.jp/ys_oota/mdp/

デザインパターンの使い方: Prototype
http://japan.internet.com/developer/20090515/26.html
最終更新:2011年11月13日 18:22