本文主要是介绍原型模式,简化多次创建相同的内容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原型模式的定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
UML图:
C++代码:
// author: Qinglong.mark.He
#include <iostream>
#include < string >
class Prototype
{
protected :
std:: string name;
public :
Prototype(){}
virtual ~Prototype(){}
virtual Prototype *clone(){ return nullptr;}
virtual void set ( char *n){}
virtual void show(){}
};
class PrototypeA: public Prototype
{
public :
PrototypeA(){
name = "PrototypeA";
}
PrototypeA( const PrototypeA &r){
name = r.name;
}
~PrototypeA(){
}
PrototypeA *clone(){
return new PrototypeA(* this );
}
void show(){
std::cout<<"PrototypeA name: "<<name<<std::endl;
}
};
yiyuan.120ask.com/news/7444/9676967.html
yiyuan.120ask.com/news/7444/9676965.html
yiyuan.120ask.com/news/7444/9676963.html
yiyuan.120ask.com/news/7444/9676979.html
yiyuan.120ask.com/news/7444/9676957.html
yiyuan.120ask.com/news/7444/9676954.html
yiyuan.120ask.com/news/7444/9676994.html
yiyuan.120ask.com/news/7444/9676990.html
yiyuan.120ask.com/news/7444/9676984.html
yiyuan.120ask.com/news/7444/9676944.html
yiyuan.120ask.com/news/7444/9676941.html
yiyuan.120ask.com/news/7444/9676939.html
yiyuan.120ask.com/news/7444/9676936.html
yiyuan.120ask.com/news/7444/9676934.html
yiyuan.120ask.com/news/7444/9676931.html
yiyuan.120ask.com/news/7444/9676927.html
yiyuan.120ask.com/news/7444/9676926.html
yiyuan.120ask.com/news/7444/9676924.html
yiyuan.120ask.com/news/7444/9676921.html
yiyuan.120ask.com/news/7444/9676919.html
class PrototypeB: public Prototype
{
public :
PrototypeB(){
name = "PrototypeB";
}
PrototypeB( const PrototypeB &r){
name = r.name;
}
~PrototypeB(){
}
PrototypeB *clone(){
return new PrototypeB(* this );
}
void show(){
std::cout<<"PrototypeB name: "<<name<<std::endl;
}
};
测试用例:
int main(){
auto r1 = new PrototypeA;
auto r2 = new PrototypeB;
auto r3 = r1->clone();
auto r4 = r2->clone();
r1->show();
r2->show();
delete r1;
delete r2;
www.aibang.com/article/444091677-443192907/product/11926341.html
www.aibang.com/show/444091677-443192907/product/11926343.html
www.aibang.com/show/444091677-443192907/product/11926345.html
www.aibang.com/show/444091677-443192907/product/11926347.html
www.aibang.com/show/444091677-443192907/product/11926349.html
www.aibang.com/article/444091677-443192907/product/11545941.html
www.aibang.com/show/444091677-443192907/product/11547029.html
www.aibang.com/show/444091677-443192907/product/11926355.html
www.aibang.com/show/444091677-443192907/product/11926357.html
www.aibang.com/show/444091677-443192907/product/11926359.html
www.aibang.com/show/444091677-443192907/product/11926377.html
www.aibang.com/show/444091677-443192907/product/10481603.html
www.aibang.com/show/444091677-443192907/product/11926385.html
www.aibang.com/show/444091677-443192907/product/11926389.html
www.aibang.com/show/444091677-443192907/product/11926395.html
www.aibang.com/show/444091677-443192907/product/11926399.html
www.aibang.com/article/444091677-443192907/product/11926401.html
www.aibang.com/show/444091677-443192907/product/11588729.html
www.aibang.com/show/444091677-443192907/product/11926409.html
www.aibang.com/show/444091677-443192907/product/10532459.html
www.aibang.com/show/444091677-443192907/product/11926423.html
www.aibang.com/show/444091677-443192907/product/11926429.html
www.aibang.com/show/444091677-443192907/product/11926431.html
www.aibang.com/show/444091677-443192907/product/11926437.html
www.aibang.com/show/444091677-443192907/product/11926439.html
www.aibang.com/show/444091677-443192907/product/11926445.html
www.aibang.com/show/444091677-443192907/product/11926449.html
www.aibang.com/show/444091677-443192907/product/11926457.html
www.aibang.com/show/444091677-443192907/product/11926463.html
www.aibang.com/show/444091677-443192907/product/11926467.html
r3->show();
r4->show();
delete r3;
delete r4;
return 0;
}
这篇关于原型模式,简化多次创建相同的内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!