Layering & Contract Philosophy With additional indirection

Prototype

The example code is as following:

  class CObject
{
public:
virtual CObject* clone() const = ;
virtual void BasicOperation() = ;
}
//clone is a virtual method, its return type can be different from base class
class CConcreteObject: public CObject
{
public:
virtual CConcreteObject* clone() const { return new CConcreteObject (*this);}
} class Client
{
public: CObject* pObject = NULL;
public:
void setObject(CObject *p)
{
if (pObject != NULL )
delete pObject;
pObject = NULL;
pObject = p->clone();
}
void operation() { pObject->BasicOperation();}
}

Applicability

  • Use the Prototype pattern when:a system should be independent of how its products are created, composed, and represented;
  • Use the Prototype pattern 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.

Participants

  • Prototype (Graphic): declares an interface for cloning itself which must be implemented by derived class.
  • ConcretePrototype (Staff, WholeNote, HalfNote): implements an operation for cloning itself.
  • Client (GraphicTool): creates a new object by asking a prototype to clone itself.

Collaborations

  • A client asks a prototype to clone itself and manipulates them as own.

From:Design Patterns:Elements of Reusable Object-Oriented Software, by GoF

Design Pattern ->Prototype的更多相关文章

  1. Design Pattern —— Prototype /Template Method/Iterator/Composite/Bridge

    Prototype /Template Method/Iterator/Composite/Bridge 为什么把这五种设计模式放一起呢,没什么太高大上的原因,就是因为这五种模式JAVA开发最基本的特 ...

  2. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  3. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  4. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  5. design pattern及其使用

    什么是设计模式? design pattern是一个通用的,可以被重用的关于一个常见的问题的解决方案. 为什么要用设计模式? 引入设计模式的理论基础非常简单.我们每天都会碰到问题.我们可能碰到决定使用 ...

  6. C++ Design Pattern: What is a Design Pattern?

    Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when deve ...

  7. Design Pattern in Simple Examples

    Instead of defining what is design pattern lets define what we mean by design and what we mean by pa ...

  8. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

  9. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

随机推荐

  1. [比赛|考试]nowcoder NOIP提高组组第二场

    160/300pts,rank16(100,30,30) 在自身找毛病,首先做题感觉还不不够认真,比如T3那个我一开始审出来了,然后tmd忘了...gg...T1AC没啥好赞美的,T2T3暴力没拿全啊 ...

  2. 「NOI.AC」Leaves 线段树合并

    题目描述 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为\(1\dots n\)的一个排列).可以任意交换每个非叶子节点的左右孩子. ...

  3. google ---gson字符串数组用GSON解析然后用逗号隔开拼接,去掉最后一个逗号

    public static void main(String[] args) { String ss= "[{\"type\":\"0\",\&quo ...

  4. 江西财经大学第一届程序设计竞赛 G

    链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 题目描述 周末,小Q喜欢在PU口袋校园上参加各种活动刷绩点,体验丰富多彩的大学生活. 但是每个活 ...

  5. linu samba服务

    关闭防火墙并且重启网络yum install samba  samba-client samba-commmon -ysystemctl start smb smbclient -L //172.25 ...

  6. java中所有开源注解收集

    @resource: resource全名为@Resource ,用来激活一个命名资源(namedresource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI conte ...

  7. servlet3

    亿级流量架构 http://www.iteye.com/blogs/subjects/as-core servlet3.1对比 http://jinnianshilongnian.iteye.com/ ...

  8. esper(3)-窗口&聚合分组

    一.Insert and Remove Stream 1. select * from com.ebc.updatelistener.User 只输出newEvents,不会输出oldEvents.即 ...

  9. word页眉添加横线与删除横线

    一.删除横线 1.打开已有页眉Word2010文档,并且页眉有横线的,双击页眉 2.选中整个页眉段落,注意:一定要选择段落标记. 3.单击菜单“开始”功能模块. 4.在“段落”中单击边框线下三角按钮. ...

  10. thinkPHP5.0验证码不显示

    1.使用composer安装时,验证码无法正常显示 主要是因为验证码扩展库的版本安装不正常,官方的5.0版本的扩展库版本号都是1.*,默认安装的是2.0版本,2.0版本均为ThinkPHP5.1版本专 ...