摘自:《设计模式精解—GoF 23种设计模式解析附C++实现源码》
URL: http://www.mscenter.edu.cn/blog/k_eckel
Prototype模式提供了一个通过已存在对象进行新对象创建的接口(Clone), Clone()实现和具体的语言相关,在C++中我们将通过拷贝构造函数实现之。
1 //Prototype.h 2 #ifndef _PROTOTYPE_H_ 3 #define _PROTOTYPE_H_ 4 5 class Prototype{ 6 public: 7 virtual ~Prototype(); 8 virtual Prototype *Clone() const = 0; 9 protected:10 Prototype();11 };12 13 class ConcretePrototype: public Prototype{14 public:15 ConcretePrototype();16 ConcretePrototype(const ConcretePrototype & cp);17 ~ConcretePrototype();18 Prototype *Clone() const;19 protected:20 private:21 };22 23 #endif //~_PROTOTYPE_H_
//Prototype.cpp#include "Prototype.h"#include <iostream>using namespace std;Prototype::Prototype(){}Prototype::~Prototype(){}Prototype *Prototype::Clone() const{ return 0;}ConcretePrototype::ConcretePrototype(){}ConcretePrototype::~ConcretePrototype(){}ConcretePrototype::ConcretePrototype(const ConcretePrototype & cp){ cout <<"ConcretePrototype Copy ..."<<endl;}Prototype *ConcretePrototype::Clone() const{ return new ConcretePrototype(*this);}
原文链接: https://www.cnblogs.com/cmleung/archive/2011/06/14/2080266.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/27199
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!