Design Pattern ->Prototype
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的更多相关文章
- Design Pattern —— Prototype /Template Method/Iterator/Composite/Bridge
Prototype /Template Method/Iterator/Composite/Bridge 为什么把这五种设计模式放一起呢,没什么太高大上的原因,就是因为这五种模式JAVA开发最基本的特 ...
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- design pattern及其使用
什么是设计模式? design pattern是一个通用的,可以被重用的关于一个常见的问题的解决方案. 为什么要用设计模式? 引入设计模式的理论基础非常简单.我们每天都会碰到问题.我们可能碰到决定使用 ...
- 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 ...
- 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 ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
随机推荐
- linux文件系统相关资料
linux下文件系统通常是通过虚拟文件系统(VFS)蔽下层具体文件系统操作的差异,为上层的操作提供一个统一的接口.文件系统底层都是用系统IO缓存层提供的块读写接口,实现逻辑块到物理块 ...
- P1969 积木大赛
题意:给你一段序列,一次操作:[l,r]内所有数+1 初始序列全为0 现在给你最后序列,问最少操作几次能达到这样的序列 蒟蒻表示秒想到------差分啊 每次差分必有一个+1,一个-1 把差分数组求出 ...
- 【Leetcode】Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List
题目来源:http://poj.org/problem?id=1056 http://poj.org/problem?id=3630 两题非常类似,所以在这里一并做了. 1056题目大意: 如果一 ...
- PowerShell命令部署WSP
转载:http://www.cnblogs.com/ChunLiangZhang/archive/2012/07/18/2597335.html(作者:ChunLiang) 现在可以用SharePoi ...
- CodeForces - 984C——Finite or not?分数整除问题(数论,gcd)
题目传送门 题目描述:给你一个p/q,让你求在b进制下,这个小数是不是有限小数. 思路: 先来膜拜一个大神的博客,如何求小数的二进制表达,(感谢博主肘子zhouzi).然后小数的其他进制表达也一样. ...
- linux 的 sftp 和 scp
====================================== 作者: wxy0327(http://wxy0327.itpub.net) 发表于: 2006.12.07 13:19 分 ...
- jQuery form插件的使用--ajaxForm()和ajaxSubmit()
转载:https://blog.csdn.net/qq_38602656/article/details/78668924
- esper(2)-事件类型
1.pojo package com.ebc.eventtype.pojo.pojo1; import cn.hutool.core.collection.CollUtil; import com.e ...
- 爬虫(GET)——爬baidu.com主页
工具:python3 目标:www.baidu.com 工作流程: 1)反爬虫第一步:抓包工具fiddler抓取页面请求信息,得到User-Agent的值,用于重构urllib.request.Req ...