设计模式之十:Prototype(原型)—对象创建型模式

中间因为一些琐事,停更了一段时间,现在补将起来。

2014-05-17 星期六 8:21:46 

Prototype,继续GOF。

可以参考:http://www.codeproject.com/Articles/185348/Prototype-Design-Pattern

1、Intent

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

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

2、Also Known As
3、Motivation


4、Applicability

Use the Prototype pattern when a system should be independent of how its products are created, composed, and 

represented; and

●  when the classes to instantiate are specified at run-time, for example, by dynamic loading; or

●  to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or

●  when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

当一个系统应该独立于它的产品创建、构成和表示时,要使用 Prototype模式;以及

 当要实例化的类是在运行时刻指定时,例如,通过动态装载;或者

  为了避免创建一个与产品类层次平行的工厂类层次时;或者

 当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。

5、Structure

6、代码

作用: 

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

抽象基类:

Prototype:虚拟基类,所有原型的基类,提供 Clone 接口函数

接口函数:

Prototype::Clone 函数:纯虚函数,根据不同的派生类来实例化创建对象.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <memory>
#include <cstring>
#include <unistd.h>
 
using  namespace  std;
 
// 虚拟基类,所有原型的基类,提供 Clone 接口函数
class  Prototype
{
public :
     virtual  ~Prototype(){};
     virtual  Prototype* Clone()  const  = 0;
     
protected :
     Prototype(){};
};
  
class  ConcreatePrototype: public  Prototype
{
public :
     ConcreatePrototype(){};
     
     ConcreatePrototype( const  ConcreatePrototype& rhs)
     {
         cout<< "Concreate prototype" <<endl;
     }
     
     Prototype* Clone()  const
     {
         cout<< "ConcreatePrototype::Clone()" <<endl;
         return  new  ConcreatePrototype(* this );
     };
};
 
class  ConcreatePrototypeB: public  Prototype
{
public :
     ConcreatePrototypeB(){};
     
     ConcreatePrototypeB( const  string& name)
         :filename(name)
     {
         cout<< "Jackill,I Hate U!" <<endl;
     }
     
     ConcreatePrototypeB( const  ConcreatePrototypeB& rhs)
         :filename(rhs.filename)
     {};
     
     Prototype* Clone()  const
     {
         cout<< "ConcreatePrototypeB::Clone(),name:" << this ->filename<<endl;
         return  new  ConcreatePrototypeB(* this );
     };
 
private :
     string filename;
};
 
 
int  main( int  argc,  char  *argv[])
{
     Prototype* p= new  ConcreatePrototype();
     Prototype* p1=p->Clone();
 
     cout<< "###########" <<endl;
 
     Prototype* p2= new  ConcreatePrototypeB( "Jackill" );
     Prototype* p3=p2->Clone();
 
     delete  p;
     delete  p1;
     delete  p2;
     delete  p3;
     return  0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值