C++设计模式2-原型模式Prototype

时间:2022-10-01 19:37:51

一 原型模式描述

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

Prototype原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,
根本无需知道任何如何创建的细节,
工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。

二 解决的问题:


    它主要面对的问题是:“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是他们却拥有比较稳定一致的接口。


三 组成

客户(Client)角色:客户端类向原型管理器提出创建对象的请求。 ①抽象原型(Prototype)角色: 这是一个抽象角色,通常由一个C#接口或抽象类实现。

②具体原型(Concrete Prototype)角色:
被复制的对象。此角色需要实现抽象的原型角色所要求的接口。

③原型管理器(Prototype Manager)角色: 创建具体原型类的对象,并记录每一个被创建的对象。

四 类图

C++设计模式2-原型模式Prototype

五 C++实现代码


①不带原型管理器



#include <iostream>
#include <cstring>



/// 原型抽象类 -=> 对应于抽象原型(Prototype)角色
class Prototype
{
public :
Prototype( ){ };

virtual ~Prototype( ){ };

virtual Prototype* Clone( ) const = 0; // 拷贝函数,原型模式的精髓所在

virtual void Show( ) const = 0; // 显示当前信息
protected :
char *m_name; // 名字
};





/// 原型类A -=> 对应于具体原型(Concrete Prototype)角色:
class PrototypeA : public Prototype
{
public :
PrototypeA(const char *name = NULL) // 构造函数
{
if(name == NULL)
{
this->m_name = new char[1];
strcpy(this->m_name, "");
}
else
{
this->m_name = new char[strlen(name) + 1];
strcpy(this->m_name, name);
}
}

PrototypeA(const PrototypeA &Prototype) // 实现深拷贝
{
this->m_name = new char[strlen(Prototype.m_name)];
strcpy(this->m_name, Prototype.m_name);
}

virtual ~PrototypeA( ) // 虚析构函数
{
delete[] this->m_name;
}

Prototype* Clone( ) const // 拷贝函数,原型模式的精髓所在
{
return new PrototypeA(*this);
}

void Show( ) const // 显示当前函数信息
{
std::cout <<"PrototypeA's name is " <<this->m_name <<std::endl;
}
//protected :
//char *m_name;
};


/// 原型类B -=> 对应于具体原型(Concrete Prototype)角色:
class PrototypeB : public Prototype
{
public :
PrototypeB(const char *name = NULL) // 构造函数
{
if(name == NULL)
{
this->m_name = new char[1];
strcpy(this->m_name, "");
}
else
{
this->m_name = new char[strlen(name) + 1];
strcpy(this->m_name, name);
}
}
PrototypeB(const PrototypeB &Prototype) //
{
this->m_name = new char[strlen(Prototype.m_name)];
strcpy(this->m_name, Prototype.m_name);
}

virtual ~PrototypeB( ) // 虚析构函数
{
delete[] this->m_name;
}
Prototype* Clone( ) const // 拷贝函数,原型模式的精髓所在
{
return new PrototypeB(*this);
} // 获取名字的函数
void Show( ) const // 显示当前函数信息
{
std::cout <<"PrototypeB's name is " <<this->m_name <<std::endl;
}
//protected :
//int *m_no;
};




int main()
{
Prototype *r1 = new PrototypeA("A");
Prototype *r2 = r1->Clone( );

// r1和r2是相同内容的副本
r1->Show( );
r2->Show( );
delete r1;
delete r2;
r1 = r2 = NULL;

Prototype *r3 = new PrototypeB("B");
Prototype *r4 = r3->Clone( );
// r1和r2是用相同内容的拷贝
r3->Show( );
r4->Show( );
delete r3;
delete r4;
r3 = r4 = NULL;

return 0;
}