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. Python 之 装饰器

    装饰器 中的“器”代指函数 所以装饰器本质是函数,用来装饰其它函数.例如:为其它函数添加其他功能 实现装饰器需要的知识:  高阶函数+嵌套函数 == 装饰器 1.函数就是“变量” 函数就是“变量”说的 ...

  2. Sublime Text3安装、配置及常用插件(陆续补全中~)

    一.安装Sublime Text3 网址:http://www.sublimetext.com/3 注册码:(sublime Text3汉化和激活注册码) ----- BEGIN LICENSE -- ...

  3. Zookeeper基础使用

    部署和运行 脚本 ZooKeeper的bin目录下的脚本见下表 脚本 说明 zkCleanup 清理ZooKeeper历史数据,包括事务日志文件和快照数据文件 zkCli ZooKeeper的一个简易 ...

  4. 合并queryset

    今天在实现搜索时遇到一个问题,如何同时搜索model里面的title以及content和category字典 contents = Blog.objects.filter(content__conta ...

  5. react 中文文档案例二 (头像时间)

    import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date ...

  6. Python:raw_input 和 input用法

    转自:http://blog.csdn.net/kjing/article/details/7450146 Python input和raw_input的区别 使用input和raw_input都可以 ...

  7. 分页插件PageHelper

    一.PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,Po ...

  8. django基础学习

    {{forloop.counter}}  这是html的自增序号 GET请求可以直接从URL中获取信息,POST请求不可以,可以把信息藏到一个隐藏的input文本框中 orm 的概念就是对象关系映射 ...

  9. spring AOP正则表达式的几个问题

    基于包名的正则表达式,是根据抽象父类的包名过滤,还是实现类的包名过滤, 还是抽象父类实现的接口的包名过滤? org.springframework.aop.aspectj.AspectJExpress ...

  10. VMware硬盘空间——扩容

    VMware原来分配的硬盘空间太小了--扩容 找到VMware的安装路径,如我是默认安装的:C:\Program Files (x86)\VMware\VMware Workstation,打开该路径 ...