设计模式学习系列6 原型模式(prototype)

原型模式(prototype)用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。允许一个对象再创建另外一个新对象的时候根本无需知道任何创建细节,只需要请求圆形对象的copy函数皆可。

1原型模式构成

clipboard

客户(Client)角色:客户类提出创建对象的请求。
抽象原型(Prototype)角色:这是一个抽象角色,C++实现的抽象类,此角色给出所有的具体原型类所需的接口。
具体原型(Concrete Prototype)角色:被复制的对象。此角色需要实现抽象原型角色所要求的接口。

2原型模式C++实现

(1)通过C++的拷贝构造函数实现

(2)clone()函数返回的类是基类,建议通过static_const<>()进行转换为子类

(3)原型模式实现过程中会涉及浅拷贝和深拷贝的问题,clone()编写的时候要注意

(4)原型模式创建新的对象简单了很多,只需要根据原型就可以获得,不过使用原型的时候内存在clone内部开辟,要记得释放

   1:   
   2:   /*设计模式学习系列之原型模式 
   3:  * 参考书籍《大话设计模式》 
   4:  * 通过明确的clone()来创造新对象,不需要知道创建的任何细节 
   5:  */ 
   6:  #include<iostream>
   7:  using namespace std ;
   8:  //接口类 
   9:  class Prototype
  10:  {
  11:  public:
  12:      virtual Prototype* Clone() const = 0 ;
  13:  };
  14:   
  15:  struct stStruct
  16:  {
  17:      int num ;
  18:      string str ;
  19:      
  20:      stStruct()
  21:      {
  22:          num= 0 ;
  23:          str = "" ;
  24:      }
  25:  };
  26:  class PrototypeA:public Prototype
  27:  {
  28:  protected:
  29:      int a ;
  30:      string str;    
  31:  public:    
  32:      PrototypeA():a(0)
  33:      {    
  34:      }
  35:      ~PrototypeA()
  36:      {    
  37:      }
  38:      //参数构造函数1 
  39:      PrototypeA(const int& _a , const string& _str ):a(_a),str(_str)
  40:      {
  41:          
  42:      }
  43:      //参数构造函数
  44:      PrototypeA(const PrototypeA& _proto)
  45:      {
  46:          a = _proto.a ;    
  47:          str = _proto.str;
  48:      } 
  49:      
  50:      //clone()函数 深拷贝 
  51:      Prototype* Clone() const
  52:      {
  53:          PrototypeA *P = new PrototypeA(*this);
  54:          return P ;
  55:      }
  56:      
  57:      void show()
  58:      {
  59:          cout << a << "---" << str << endl;
  60:      }
  61:      
  62:      void SetA(const int& _a)
  63:      {
  64:          a = _a ;
  65:      }
  66:      
  67:      void SetStr(const string& _str)
  68:      {
  69:          str = _str ;
  70:      }
  71:  };
  72:   
  73:  int main()
  74:  {
  75:      PrototypeA *test = new PrototypeA(1,"xxxx");
  76:      
  77:      //通过clone()创建 
  78:      PrototypeA *test_clone = static_cast<PrototypeA *>(test->Clone());
  79:      
  80:      //通过拷贝构造函数创建
  81:      PrototypeA *test2 = new PrototypeA(*test); 
  82:      
  83:      cout << "===============赋值结束"  << endl ;
  84:      test->show();
  85:      test_clone->show();
  86:      test2->show();
  87:      
  88:      cout << "===============修改值类型"  << endl ;
  89:      test->SetA(3);
  90:      test->show();
  91:      test_clone->show();
  92:      test2->show();    
  93:      
  94:      cout << "===============修改字符类型"  << endl ;    
  95:      test->SetStr("343245");
  96:      test->show();
  97:      test_clone->show();
  98:      test2->show();
  99:  }

3涉及到的C++知识点

(1)c++深拷贝和浅拷贝http://www.2cto.com/kf/201205/133802.html

(2)C++类 拷贝赋值构造函数http://blog.chinaunix.net/uid-25808509-id-354211.html

细雨淅淅 标签: 设计 模式
posted @ 2013-11-07 13:38  RubbyZhang  阅读(334)  评论(0编辑  收藏  举报