prototype.cpp

来自「这是一些c++例程」· C++ 代码 · 共 41 行

CPP
41
字号
namespace DesignPattern_Prototype
{
	//定义抽象基类class Prototype
	class Prototype	{
	public:
		virtual Prototype* Clone() = 0 ;
	} ;
	//定义 class ConcretePrototype1
	class ConcretePrototype1 : public Prototype
	{
	public:
		virtual Prototype* Clone()
		{
			ConcretePrototype1 *p = new ConcretePrototype1() ;
			*p = *this ; //复制对象
			return p ;
		}
	} ;
	//定义class ConcretePrototype2
	class ConcretePrototype2 : public Prototype
	{
	public:
		virtual Prototype* Clone()
		{
			ConcretePrototype2 *p = new ConcretePrototype2() ;
			*p = *this ; //复制对象
			return p ;
		}
	} ;
}
void main()
{
	using namespace DesignPattern_Prototype ;
	ConcretePrototype1 *obj1 = new ConcretePrototype1() ;//构造原型对象1
	ConcretePrototype2 *obj2 = new ConcretePrototype2() ;//构造原型对象2
	Prototype *newobj1 = obj1->Clone() ;//克隆对象1
	Prototype *newobj2 = obj2->Clone() ;//克隆对象2
	//使用复制出的对象newobj1和newobj2……………..
 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?