设计模式之十:Prototype(原型)—对象创建型模式
来源:互联网 发布:有限元法的软件 编辑:程序博客网 时间:2024/04/30 10:00
中间因为一些琐事,停更了一段时间,现在补将起来。
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 函数:纯虚函数,根据不同的派生类来实例化创建对象.
#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;
}
- 设计模式之十:Prototype(原型)—对象创建型模式
- 设计模式之对象创建型 — prototype 原型模式
- 设计模式之Prototype(原型模式)对象创建型
- 设计模式学习之--PROTOTYPE(原型)对象创建型模型
- 设计模式之原型法(ProtoType)----对象创建型模式
- 面向对象设计模式之Prototype原型模式(创建型)
- 设计模式——创建型模式之原型模式(Prototype Pattern)
- (创建模式)设计模式之Prototype(原型)
- 设计模式:创建型模式:原型模式(prototype pattern)
- 设计模式笔记--创建型模式之四--原型prototype
- 设计模式(创建型)之原型模式(Prototype Pattern)
- 【设计模式】创建型模式之原型Prototype
- 创建型设计模式---原型模式(Prototype)
- PROTOTYPE(原型)—对象创建型模式
- 【创建型模式】原型模式(Prototype)之23种java设计模式
- Java设计模式之创建型模式-原型模式(Prototype)
- 原型模式(Prototype Pattern对象创建型模式)
- 创建型模式之原型(ProtoType)模式
- 嵌入式二维码
- 使用Fiddler插件X5S寻找XSS漏洞
- 雕刻机覅而雷克萨斯
- MyEclipse 常用快捷键列表,实用的快捷键大全
- 插入排序
- 设计模式之十:Prototype(原型)—对象创建型模式
- iOS 数据 类型转换 例子
- JAVA 学习日志 测试时间Date、日历Canlendar、时间格式SinmpleDateFormat。珍惜时间,珍爱生命
- 自定义ScrollView实现反弹效果(以及解决和ListView之间的冲突)
- 数据库面试(持续更新)
- 如何将Myeclipse项目发布到eclipse上面去
- OCP 1Z0 051 160
- 设计模式之十一:Composite(组合)—对象结构型模式
- 自己的ScrollView实现反弹效果