原型模式

来源:互联网 发布:手机 Ubuntu 编辑:程序博客网 时间:2024/06/07 15:33
1.概述
定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

其中有一个词很重要,那就是拷贝。可以说,拷贝是原型模式的精髓所在。举个现实中的例子来介绍原型模式。找工作的时候,我们需要准备简历。假设没有打印设备,因此需手写简历,这些简历的内容都是一样的。这样有个缺陷,如果要修改简历中的某项,那么所有已写好的简历都要修改,工作量很大。随着科技的进步,出现了打印设备。我们只需手写一份,然后利用打印设备复印多份即可。如果要修改简历中的某项,那么修改原始的版本就可以了,然后再复印。原始的那份手写稿相当于是一个原型,有了它,就可以通过复印(拷贝)创造出更多的新简历。这就是原型模式的基本思想。


2.UML图


3.代码实现

#include <iostream>using namespace std;/*解析:Prototype模式其实就是常说的"虚拟构造函数"一个实现,C++的实现机制中并没有支持这个特性,但是通过不同派生类实现的Clone接口函数可以完成与"虚拟构造函数"同样的效果.举一个例子来解释这个模式的作用,假设有一家店铺是配钥匙的,他对外提供配制钥匙的服务(提供Clone接口函数),你需要配什么钥匙它不知道只是提供这种服务,具体需要配什么钥匙只有到了真正看到钥匙的原型才能配好.也就是说,需要一个提供这个服务的对象,同时还需要一个原型(Prototype),不然不知道该配什么样的钥匙.*/#define SAFE_RELEASE(_p){ if((_p)){ delete (_p); (_p) = NULL;} }//声明一个虚拟基类,所有的原型都从这个基类继承class Prototype{public:Prototype() { cout<<"Prototype contruct"<<endl; }Prototype(const Prototype& other) { cout<<"Prototype copy contruct"<<endl; }virtual ~Prototype() { cout<<"Prototype destruct"<<endl; }virtual Prototype* Clone() = 0;};class ConcretePrototype1 : public Prototype{public:ConcretePrototype1() { cout<<"ConcretePrototype1 contruct"<<endl; }ConcretePrototype1(const ConcretePrototype1& other) { cout<<"ConcretePrototype1 copy contruct"<<endl; }virtual ~ConcretePrototype1() { cout<<"ConcretePrototype1 destruct"<<endl; }virtual ConcretePrototype1* Clone() { return new ConcretePrototype1(*this); }//调用ConcreatePrototype1的复制构造函数};class ConcretePrototype2 : public Prototype{public:ConcretePrototype2() { cout<<"ConcretePrototype2 contruct"<<endl; }ConcretePrototype2(const ConcretePrototype1& other) { cout<<"ConcretePrototype2 copy contruct"<<endl; }virtual ~ConcretePrototype2() { cout<<"ConcretePrototype2 destruct"<<endl; }virtual ConcretePrototype2* Clone() { return new ConcretePrototype2(*this); }//调用ConcreatePrototype2的复制构造函数};int main(int argc, char* argv[]){Prototype* p = new ConcretePrototype1();Prototype* pClone = p->Clone();//.....SAFE_RELEASE(p);SAFE_RELEASE(pClone);return 0;}  

原型模式实现的关键就是实现Clone函数,对于C++来说,其实就是拷贝构造函数,需实现深拷贝,下面给出一种实现。
#include <iostream>using namespace std;#define SAFE_RELEASE(_p){ if((_p)){ delete (_p); (_p) = NULL;} }class Resume{protected:char *name;public:Resume() {}Resume(const Resume& other) {}virtual ~Resume() {}virtual Resume* Clone() = 0;virtual void Show() {}};class ResumeA : public Resume{public://构造函数ResumeA(const char *str){if (str != NULL){int nLen = strlen(str)+1;name = new char[nLen];memset(name, 0x0, nLen);strcpy(name, str);} else{name = new char[1];name[0] = '\0';}}//拷贝构造函数ResumeA(const ResumeA &r){int nLen = strlen(r.name)+1;name = new char[nLen];memset(name, 0x0, nLen);strcpy(name, r.name);}//析构函数~ResumeA(){delete[] name;}//克隆,关键所在ResumeA* Clone(){return new ResumeA(*this);}//显示内容void Show(){cout<<"ResumeA name : "<<name<<endl;}};int main(int argc, char* argv[]){Resume *r1 = new ResumeA("A");Resume *r2 = r1->Clone();r1->Show();SAFE_RELEASE(r1);r2->Show();SAFE_RELEASE(r2);return 0;}  

其实这个设计模式比较简单,我们总结一下具体操作步骤。
1)声明一个抽象基类,并定义clone()函数为纯虚函数。
2)实例化各个子类,并且实现复制构造函数,并实现clone()函数 
0 0
原创粉丝点击