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

简介:

一 原型模式描述

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

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

二 解决的问题:


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


三 组成

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

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

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

四 类图



五 C++实现代码


①不带原型管理器



  1. #include <iostream>  
  2. #include <cstring>  
  3.   
  4.   
  5.   
  6. /// 原型抽象类 -=> 对应于抽象原型(Prototype)角色  
  7. class Prototype  
  8. {  
  9. public :  
  10.     Prototype( ){  };  
  11.   
  12.     virtual ~Prototype( ){ };  
  13.   
  14.     virtual Prototype* Clone( ) const = 0;  // 拷贝函数,原型模式的精髓所在  
  15.   
  16.     virtual void Show( ) const = 0;                // 显示当前信息  
  17. protected :  
  18.     char *m_name;               // 名字  
  19. };  
  20.   
  21.   
  22.   
  23.   
  24.   
  25. /// 原型类A -=> 对应于具体原型(Concrete Prototype)角色:  
  26. class PrototypeA : public Prototype  
  27. {  
  28. public :  
  29.     PrototypeA(const char *name = NULL)           // 构造函数  
  30.     {  
  31.         if(name == NULL)  
  32.         {  
  33.             this->m_name = new char[1];  
  34.             strcpy(this->m_name, "");  
  35.         }  
  36.         else  
  37.         {  
  38.             this->m_name = new char[strlen(name) + 1];  
  39.             strcpy(this->m_name, name);  
  40.         }  
  41.     }  
  42.   
  43.     PrototypeA(const PrototypeA &Prototype)               // 实现深拷贝  
  44.     {  
  45.         this->m_name = new char[strlen(Prototype.m_name)];  
  46.         strcpy(this->m_name, Prototype.m_name);  
  47.     }  
  48.   
  49.     virtual ~PrototypeA( )                 // 虚析构函数  
  50.     {  
  51.         delete[] this->m_name;  
  52.     }  
  53.   
  54.     Prototype* Clone( ) const  // 拷贝函数,原型模式的精髓所在  
  55.     {  
  56.         return new PrototypeA(*this);  
  57.     }  
  58.   
  59.     void Show( ) const                // 显示当前函数信息  
  60.     {  
  61.     std::cout <<"PrototypeA's name is " <<this->m_name <<std::endl;  
  62.     }  
  63. //protected :  
  64. //  char *m_name;  
  65. };  
  66.   
  67.   
  68. /// 原型类B -=> 对应于具体原型(Concrete Prototype)角色:  
  69. class PrototypeB : public Prototype  
  70. {  
  71. public :  
  72.     PrototypeB(const char *name = NULL)          // 构造函数  
  73.     {  
  74.     if(name == NULL)  
  75.     {  
  76.         this->m_name = new char[1];  
  77.         strcpy(this->m_name, "");  
  78.     }  
  79.     else  
  80.     {  
  81.         this->m_name = new char[strlen(name) + 1];  
  82.         strcpy(this->m_name, name);  
  83.     }  
  84.     }  
  85.     PrototypeB(const PrototypeB &Prototype)               //  
  86.     {  
  87.         this->m_name = new char[strlen(Prototype.m_name)];  
  88.         strcpy(this->m_name, Prototype.m_name);  
  89.     }  
  90.   
  91.     virtual ~PrototypeB( )                // 虚析构函数  
  92.     {  
  93.         delete[] this->m_name;  
  94.     }  
  95.     Prototype* Clone( ) const  // 拷贝函数,原型模式的精髓所在  
  96.     {  
  97.         return new PrototypeB(*this);  
  98.     }                      // 获取名字的函数  
  99.     void Show( ) const                // 显示当前函数信息  
  100.     {  
  101.     std::cout <<"PrototypeB's name is " <<this->m_name <<std::endl;  
  102.     }  
  103. //protected :  
  104. //  int     *m_no;  
  105. };  
  106.   
  107.   
  108.   
  109.   
  110. int main()  
  111. {  
  112.     Prototype *r1 = new PrototypeA("A");  
  113.     Prototype *r2 = r1->Clone( );  
  114.   
  115.     // r1和r2是相同内容的副本  
  116.     r1->Show( );  
  117.     r2->Show( );  
  118.     delete r1;  
  119.     delete r2;  
  120.     r1 = r2 = NULL;  
  121.   
  122.     Prototype *r3 = new PrototypeB("B");  
  123.     Prototype *r4 = r3->Clone( );  
  124.     // r1和r2是用相同内容的拷贝  
  125.     r3->Show( );  
  126.     r4->Show( );  
  127.     delete r3;  
  128.     delete r4;  
  129.     r3 = r4 = NULL;  
  130.   
  131.     return 0;  
  132. }  


转载:http://blog.csdn.net/gatieme/article/details/17960555

目录
相关文章
|
25天前
|
设计模式 安全 测试技术
【C/C++ 设计模式 单例】单例模式的选择策略:何时使用,何时避免
【C/C++ 设计模式 单例】单例模式的选择策略:何时使用,何时避免
59 0
|
28天前
|
设计模式 算法 C++
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程(二)
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程
26 0
|
30天前
|
设计模式 存储 uml
C++ 设计模式实战:外观模式和访问者模式的结合使用,派生类访问基类的私有子系统
C++ 设计模式实战:外观模式和访问者模式的结合使用,派生类访问基类的私有子系统
27 1
|
7天前
|
设计模式 Java
小谈设计模式(10)—原型模式
小谈设计模式(10)—原型模式
|
23天前
|
设计模式 算法 中间件
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
109 1
|
25天前
|
设计模式 关系型数据库 数据库
【C++ 设计模式 工厂模式对比】深入探索设计模式:工厂方法与抽象工厂的比较与对照
【C++ 设计模式 工厂模式对比】深入探索设计模式:工厂方法与抽象工厂的比较与对照
21 1
|
28天前
|
设计模式 存储 算法
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程(三)
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程
24 0
|
28天前
|
设计模式 算法 搜索推荐
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程(一)
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程
37 0
|
11天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0
|
30天前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0