Layering & Contract Philosophy With additional indirection

Prototype

The example code is as following:

  1. class CObject
  2. {
  3. public:
  4. virtual CObject* clone() const = ;
  5. virtual void BasicOperation() = ;
  6. }
  7. //clone is a virtual method, its return type can be different from base class
  8. class CConcreteObject: public CObject
  9. {
  10. public:
  11. virtual CConcreteObject* clone() const { return new CConcreteObject (*this);}
  12. }
  13.  
  14. class Client
  15. {
  16. public: CObject* pObject = NULL;
  17. public:
  18. void setObject(CObject *p)
  19. {
  20. if (pObject != NULL )
  21. delete pObject;
  22. pObject = NULL;
  23. pObject = p->clone();
  24. }
  25. void operation() { pObject->BasicOperation();}
  26. }

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. Django个人博客开发 | 前言

    本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...

  2. P4768 [NOI2018]归程

    \(\color{#0066ff}{题目描述}\) 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 n 个节点.m 条边的无向连通图(节点的编号从 1 至 n) ...

  3. storm本地可以运行集群出错遇到的问题

    storm本地运行和集群运行是存在区别的: 本地可以读取本地文件系统及java项目中的文件,但是提交集群后就不能读取了,storm只是将topology提交到了集群,所以只能在main方法中将需要读取 ...

  4. 2.mybatis 的列名与数据库字段不对应

    mybatis 的列名与数据库字段不对应 1.别名 映射文件 : BlogMapper.xml <mapper namespace="com.xms.mybatis.mapper.Bl ...

  5. 001 开发环境搭建、安卓项目结构、R文件位置、asset目录创建

    1.安卓开发平台搭建 (1)下载SDK基础工具包(自己的百度云中) (2)将下载的安装包(android-sdk_r24.4.1-windows.zip)解压后,放到以下路径 C:\SoftAppli ...

  6. Django 07 Django模型基础2 (常用查询和多表关联)

    Django 07 Django模型基础2 (常用查询和多表关联) 一.常用查询 #查找数据 def search_user(request): #获取 rs = User.objects.first ...

  7. BAPC 2014:Button Bashing(暴力+bfs)

    题意: 给出n,m,代表微波炉有n个按钮,要求达到总时间为m 然后给出n个数,代表n个按钮能增加的时间,问最少几步,能够使得按出的总时间大于等于要求的时间,并且相差最小 输出最小的步数与相差的最小值 ...

  8. POJ3469Dual Core CPU

    题意:给你n个模块,每个模块在A核花费为ai,在B核跑花费为bi,然后由m个任务(ai,bi,wi),表示如果ai,bi不在同一个核上跑,额外的花费为wi,求最小的花费. 分析: 用最小的费用将对象划 ...

  9. python3 + pycharm+requests+HTMLTestRunner生成不了测试报告html

    生成不了测试文件,是运行的方式不对.因为在运行的时候,pycharm默认使用unit-test运行,所以没有生成测试报告.至于为什么会这样子,我就不清楚了,不过想了解更多的朋友,可以百度一下. 解决的 ...

  10. WIN10安装SW2012完整破解文件和问题解决Crack

    Win10在安装solidword后,激活时有可能报错,本文介绍怎么解决问题 安装后激活报错信息: 一.按下面步骤进行安装 1.相关序列号,记住要断网进行安装 Install SolidWorks 2 ...