原型模式

定义:

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

解决什么问题:

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

带Prototype Manager的原型模式:

客户(Client)角色:客户端类向原型管理器提出创建对象的请求。 [1]
抽象原型(Prototype)角色:这是一个抽象角色,通常由一个C#接口或抽象类实现。此角色给出所有的具体原型类所需的接口。在C#中,抽象原型角色通常实现了ICloneable接口。
具体原型(Concrete Prototype)角色:被复制的对象。此角色需要实现抽象的原型角色所要求的接口。
原型管理器(Prototype Manager)角色:创建具体原型类的对象,并记录每一个被创建的对象。
代码实现如下:
/ CplusplusPrototype.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<assert.h>
usingnamespace std;
//父类
class Resume
{
protected:
char *name;
public:
Resume() {}
virtual ~Resume() {}
virtual Resume* Clone() { return NULL; }
virtualvoid Set(char *n) {}
virtualvoid Show() {}
};
class ResumeA : public Resume
{
public:
ResumeA(constchar *str); //构造函数
ResumeA(const ResumeA &r); //拷贝构造函数
~ResumeA(); //析构函数
ResumeA* Clone(); //克隆,关键所在
void Show(); //显示内容
};
ResumeA::ResumeA(constchar *str)
{
if(str == NULL) {
name = newchar[1];
name[0] = '\0';
}
else {
name = newchar[strlen(str)+1];
strcpy(name, str);
}
}
ResumeA::~ResumeA() { delete [] name;}
ResumeA::ResumeA(const ResumeA &r) {
name = newchar[strlen(r.name)+1];
strcpy(name, r.name);
}
ResumeA* ResumeA::Clone() {
returnnew ResumeA(*this);
}
void ResumeA::Show() {
cout<<"ResumeA name : "<<name<<endl;
}
class ResumeB : public Resume
{
public:
ResumeB(constchar *str); //构造函数
ResumeB(const ResumeB &r); //拷贝构造函数
~ResumeB(); //析构函数
ResumeB* Clone(); //克隆,关键所在
void Show(); //显示内容
};
ResumeB::ResumeB(constchar *str)
{
if(str == NULL) {
name = newchar[1];
name[0] = '\0';
}
else {
name = newchar[strlen(str)+1];
strcpy(name, str);
}
}
ResumeB::~ResumeB() { delete [] name;}
ResumeB::ResumeB(const ResumeB &r) {
name = newchar[strlen(r.name)+1];
strcpy(name, r.name);
}
ResumeB* ResumeB::Clone() {
returnnew 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值