解决的问题:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。这个其实和C++的拷贝构造函数的作用是一致的,实际上就是动态抽取当前对象运行时的状态。
类图结构:
客户(Client)角色:客户类提出创建对象的请求。
抽象原型(Prototype)角色:这是一个抽象角色,通常由一个C#接口或抽象类实现。此角色给出所有的具体原型类所需的接口。在C#中,抽象原型角色通常实现了ICloneable接口。
具体原型(ConcretePrototype)角色:被复制的对象。此角色需要实现抽象原型角色所要求的接口。
样例实现:
例子参照wuzhekai1985的简历的例子,代码拷贝如下:
// CplusplusPrototype.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;
//父类
class Resume
{
protected:
char *name;
public:
Resume() {}
virtual ~Resume() {}
virtual Resume* Clone() { return NULL; }
virtual void Set(char *n) {}
virtual void Show() {}
};
class ResumeA : public Resume
{
public:
ResumeA(const char *str); //构造函数
ResumeA(const ResumeA &r); //拷贝构造函数
~ResumeA(); //析构函数
ResumeA* Clone(); //克隆,关键所在
void Show(); //显示内容
};
ResumeA::ResumeA(const char *str)
{
if(str == NULL) {
name = new char[1];
name[0] = '\0';
}
else {
name = new char[strlen(str)+1];
strcpy(name, str);
}
}
ResumeA::~ResumeA() { delete [] name;}
ResumeA::ResumeA(const ResumeA &r) {
name = new char[strlen(r.name)+1];
strcpy(name, r.name);
}
ResumeA* ResumeA::Clone() {
return new ResumeA(*this);
}
void ResumeA::Show() {
cout<<"ResumeA name : "<<name<<endl;
}
class ResumeB : public Resume
{
public:
ResumeB(const char *str); //构造函数
ResumeB(const ResumeB &r); //拷贝构造函数
~ResumeB(); //析构函数
ResumeB* Clone(); //克隆,关键所在
void Show(); //显示内容
};
ResumeB::ResumeB(const char *str)
{
if(str == NULL) {
name = new char[1];
name[0] = '\0';
}
else {
name = new char[strlen(str)+1];
strcpy(name, str);
}
}
ResumeB::~ResumeB() { delete [] name;}
ResumeB::ResumeB(const ResumeB &r) {
name = new char[strlen(r.name)+1];
strcpy(name, r.name);
}
ResumeB* ResumeB::Clone() {
return new ResumeB(*this);
}
void ResumeB::Show() {
cout<<"ResumeB name : "<<name<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Resume *r1 = new ResumeA("A");
Resume *r2 = new ResumeB("B");
Resume *r3 = r1->Clone();
Resume *r4 = r2->Clone();
r1->Show(); r2->Show();
//删除r1,r2
delete r1; delete r2;
r1 = r2 = NULL;
//深拷贝所以对r3,r4无影响
r3->Show(); r4->Show();
delete r3; delete r4;
r3 = r4 = NULL;
return 0;
}
带PrototypeManager的原型模式:
客户(Client)角色:客户端类向原型管理器提出创建对象的请求。
抽象原型(Prototype)角色:这是一个抽象角色,通常由一个C#接口或抽象类实现。此角色给出所有的具体原型类所需的接口。在C#中,抽象原型角色通常实现了ICloneable接口。
具体原型(ConcretePrototype)角色:被复制的对象。此角色需要实现抽象的原型角色所要求的接口。
原型管理器(PrototypeManager)角色:创建具体原型类的对象,并记录每一个被创建的对象。
代码实现如下:
// CplusplusPrototype.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;
//父类
class Resume
{
protected:
char *name;
public:
Resume() {}
virtual ~Resume() {}
virtual Resume* Clone() { return NULL; }
virtual void Set(char *n) {}
virtual void Show() {}
};
class ResumeA : public Resume
{
public:
ResumeA(const char *str); //构造函数
ResumeA(const ResumeA &r); //拷贝构造函数
~ResumeA(); //析构函数
ResumeA* Clone(); //克隆,关键所在
void Show(); //显示内容
};
ResumeA::ResumeA(const char *str)
{
if(str == NULL) {
name = new char[1];
name[0] = '\0';
}
else {
name = new char[strlen(str)+1];
strcpy(name, str);
}
}
ResumeA::~ResumeA() { delete [] name;}
ResumeA::ResumeA(const ResumeA &r) {
name = new char[strlen(r.name)+1];
strcpy(name, r.name);
}
ResumeA* ResumeA::Clone() {
return new ResumeA(*this);
}
void ResumeA::Show() {
cout<<"ResumeA name : "<<name<<endl;
}
class ResumeB : public Resume
{
public:
ResumeB(const char *str); //构造函数
ResumeB(const ResumeB &r); //拷贝构造函数
~ResumeB(); //析构函数
ResumeB* Clone(); //克隆,关键所在
void Show(); //显示内容
};
ResumeB::ResumeB(const char *str)
{
if(str == NULL) {
name = new char[1];
name[0] = '\0';
}
else {
name = new char[strlen(str)+1];
strcpy(name, str);
}
}
ResumeB::~ResumeB() { delete [] name;}
ResumeB::ResumeB(const ResumeB &r) {
name = new char[strlen(r.name)+1];
strcpy(name, r.name);
}
ResumeB* ResumeB::Clone() {
return new ResumeB(*this);
}
void ResumeB::Show() {
cout<<"ResumeB name : "<<name<<endl;
}
class ResumeManager
{
private:
vector<Resume *> mResume;
public:
ResumeManager()
{
}
void add(Resume * resume)
{
mResume.push_back(resume);
}
Resume * get(int index) const
{
assert(index>=0 && index<mResume.size());
return mResume[index];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ResumeManager *manager = new ResumeManager();
Resume *r1 = new ResumeA("A");
Resume *r2 = new ResumeB("B");
manager->add(r1);
manager->add(r2);
manager->get(0)->Show();
manager->get(1)->Show();
Resume *r3 = manager->get(0)->Clone();
Resume *r4 = manager->get(1)->Clone();
//删除r1,r2
delete r1; delete r2;
r1 = r2 = NULL;
//深拷贝所以对r3,r4无影响
r3->Show(); r4->Show();
delete r3; delete r4;
r3 = r4 = NULL;
return 0;
}
实现要点:
1.使用原型管理器,体现在一个系统中原型数目不固定时,可以动态的创建和销毁。
2.实现克隆操作,在.NET中可以使用Object类的MemberwiseClone()方法来实现对象的浅表拷贝或通过序列化的方式来实现深拷贝,在C++中就是拷贝构造函数的作用。
3.Prototype模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求这些“易变类”拥有稳定的接口。
效果:
1.它对客户隐藏了具体的产品类,因此减少了客户知道的名字的数目。
2.Prototype模式允许客户只通过注册原型实例就可以将一个具体产品类并入到系统中,客户可以在运行时刻建立和删除原型。
3.减少了子类构造,Prototype模式是克隆一个原型而不是请求工厂方法创建一个,所以它不需要一个与具体产品类平行的Creater类层次。
4.Portotype模式具有给一个应用软件动态加载新功能的能力。由于Prototype的独立性较高,可以很容易动态加载新功能而不影响老系统。
5.产品类不需要非得有任何事先确定的等级结构,因为Prototype模式适用于任何的等级结构
6.Prototype模式的最主要缺点就是每一个类必须配备一个克隆方法。而且这个克隆方法需要对类的功能进行通盘考虑,这对全新的类来说不是很难,但对已有的类进行改造时,不一定是件容易的事。
适用性:
1.当一个系统应该独立于他的产品创建、构成和表示时,需要使用原型模式
2.当要实例化的类是在运行时刻指定时,如通过动态装载
3.为了避免创建一个与产品类层次平行的工厂类层次时
4.当一个类的实例只能有几个不同状态组合中的一种时,建立相应数目的原型并克隆他们可能比每次用合适的状态手工实例化该类更方便一些。
分享到:
相关推荐
设计模式C++学习之原型模式(Prototype)
就像在一年前看GoF的《设计模式》一书,和半年前用C++模拟、实现23种经典的设计模式一般,透过这个写解析的过程,我又看到了另外一个境界。一直认为学习的过程很多时候可以这样划分:自己学会一门知识(技术)、表达...
我所理解的设计模式 C++实现
6、原型模式(Prototype Pattern) 用意:通过拷贝创建一个新的对象
对应的博客链接:http://blog.csdn.net/t1234xy4/article/details/52432559#
23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm” 供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) ...
设计模式C++实现(7)——外观模式、组合模式[参照].pdf
设计模式精解-GoF23种设计模式解析附C++实现源码,是系统学习设计模式,深入理解每种模式的原型,优缺点,适用的场景,达到灵活运用目的的最好参考书
一本专门介绍设计模式的好书,基于C++的案例,该书深入浅出地介绍了设计模式的概念及应用。
生死枪战游戏目前以3个模式运行(持续更新),玩家可以通过自己的喜好来选择,如:新手训练营,无限闯关,打怪,以及能调用时间而改变战力的暗月阁长老。更多特殊技能、特殊武器等你发掘! 改进或交流该游戏的建议...
虚拟机的设计与实现——C_C++虚拟机的设计与实现——C_C++虚拟机的设计与实现——C_C++虚拟机的设计与实现——C_C++虚拟机的设计与实现——C_C++
深入理解C++11——C++11新特性解析与应用。已加入章节标签,方便读者查阅。
Head First 设计模式 (八) 模板方法模式(Template Method pattern) C++实现
Head First 设计模式 (五) 单件模式(Singleton pattern) C++实现
设计模式是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代 码可靠性。...本资源使用C++实现了23种设计模式的经典案例。
C++资源——实验讲义C++资源——实验讲义
Head First 设计模式 (六) 命令模式(Command pattern) C++实现
基于MFC的BP神经网络实现的简单模式识别,欢迎各位下载学习。
C++ 23种设计模式(全_解释+源代码).zip 23种设计模式都是基于以下原则,务必记牢 依赖倒转原则 里氏代换原则 开闭原则 接口隔离原则 合成聚合复用原则 迪米特法则 23种设计模式如下: