意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

实用性:1.当要实例化的类是在运行时刻指定时。

    2.为了避免创建一个与产品类层次平行的工厂类层次时。

    3.当一个类的实例只能有几个不同状态组合中的一种时。

效果:   1.可以在运行时刻增加产品。

    2.改变值以指定新对象。

    3.改变结构以指定新对象。

    4.减少子类的构造。

    5.用类动态配置应用。

结构:

原型模式让我们不用重新初始化对象,而是动态地获得对象运行时的状态,并且在不确定对象类型的时候能

创建出对应新对象。

代码实例:

#ifndef _Prototype_
#define _Prototype_
#include <string>
#include <iostream>
using namespace std;
class AbsGoods{
public:
virtual AbsGoods* Clone() = 0;
string _mName;
protected:
AbsGoods(const AbsGoods& another){}
~AbsGoods(){}
AbsGoods(){} int _Price;
};
class Mp3:public AbsGoods{
public:
Mp3(){_mName = "MP3"; _Price = 200;}
Mp3(const Mp3& another)
{
_mName = another._mName;
_Price = another._Price;
}
virtual AbsGoods* Clone()
{
return new Mp3(*this);
}
};
class Computer:public AbsGoods{
public:
Computer(const Computer& another)
{
_mName = another._mName;
_Price = another._Price;
}
Computer(){_mName = "COMPUTER"; _Price = 4000;}
virtual AbsGoods* Clone()
{
return new Computer(*this);
}
};
class Person{
public:
Person(){_CountGoods = 0;}
bool addGoods(AbsGoods* aGoods)
{
if(NULL == aGoods) return false;
if(_CountGoods >= 10)
return false;
_myGoods[_CountGoods] = aGoods;
_CountGoods++;
return true;
}
void out()
{
for(int i=0; i<_CountGoods; i++)
{
cout<<_myGoods[i]->_mName<<" ";
}
}
AbsGoods* getGoods(int Num)
{
return _myGoods[Num-1];
}
private:
AbsGoods* _myGoods[10];
int _CountGoods;
};
class Mical : public Person{
public:
static Mical* getMical()
{
if(NULL == _thisMical)
{
_thisMical = new Mical;
}
return _thisMical;
}
private:
Mical(){}
Mical(const Mical& another){}
void operator = (const Mical& another);
static Mical* _thisMical;
};
class Merry : public Person{
public:
static Merry* getMerry()
{
if(NULL == _thisMerry)
{
_thisMerry = new Merry;
}
return _thisMerry;
}
private:
Merry(){}
Merry(const Merry& another){}
void operator = (const Merry& another);
static Merry* _thisMerry;
};
#endif
 

现Mical有一台电脑和一架Mp3

mical->addGoods(new Computer);
mical->addGoods(new Mp3);

然后Merry觉得Mical有的我也要有

for(int i=0; i<mical->getGoodsNum(); i++)
{
merry->addGoods(mical->getGoods(i+1)->Clone());
}

这里实现了不确定对象类型的时候能创建出对应新对象

mian函数代码如下:

#include <iostream>
using namespace std;
#include "Prototype.h"
Mical* Mical::_thisMical = NULL;
Merry* Merry::_thisMerry = NULL;
int main()
{
Mical* mical = Mical::getMical();
Merry* merry = Merry::getMerry(); mical->addGoods(new Computer);
mical->addGoods(new Mp3); for(int i=0; i<mical->getGoodsNum(); i++)
{
merry->addGoods(mical->getGoods(i+1)->Clone());
}
cout<<"Mical's Goods : ";
mical->out();
cout<<endl;
cout<<"Merry's Goods : ";
merry->out();
return 0;
}

原型模式(Prototype)C++实现的更多相关文章

  1. Net设计模式实例之原型模式( Prototype Pattern)

    一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...

  2. 二十四种设计模式:原型模式(Prototype Pattern)

    原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...

  3. 设计模式(四)原型模式Prototype(创建型)

      设计模式(四)原型模式Prototype(创建型) 1.   概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...

  4. 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

    原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...

  5. 原型模式-Prototype(Java实现)

    原型模式-Prototype 通过复制(克隆.拷贝)一个指定类型的对象来创建更多同类型的对象. 就像去蛋糕店买蛋糕一样. 柜台里的蛋糕都是非卖品. 只是为顾客提供一种参照. 当顾客看上某一个样式的蛋糕 ...

  6. 原型模式 prototype 创建型 设计模式(七)

    原型模式  prototype 意图 用原型实例指定需要创建的对象的类型,然后使用复制这个原型对象的方法创建出更多同类型的对象   显然,原型模式就是给出一个对象,然后克隆一个或者更多个对象 小时候看 ...

  7. PHP设计模式 原型模式(Prototype)

    定义 和工厂模式类似,用来创建对象.但实现机制不同,原型模式是先创建一个对象,采用clone的方式进行新对象的创建. 场景 大对象的创建. 优点 1.可以在运行时刻增加和删除产品 2.可以改变值或结构 ...

  8. 设计模式系列之原型模式(Prototype Pattern)——对象的克隆

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  9. 六个创建模式之原型模式(Prototype Pattern)

    定义: 使用原型实例指定创建对象的种类,并通过拷贝这个原型的属性创建新的对象. 结构图: Prototype:抽象原型类,声明克隆方法的接口,并是所有原型类的公共父类.在Java中,Object类为该 ...

  10. [工作中的设计模式]原型模式prototype

    一.模式解析 提起prototype,最近看多了js相关的内容,第一印象首先是js的原型 var Person=function(name){ this.name=name; } Person.pro ...

随机推荐

  1. sessionStorage和localStorage存储的转换不了json

    先说说localStorage与sessionStorage的差别 sessionStorage是存储浏览器的暂时性的数据,当关闭浏览器下次再打开的时候就不能拿到之前存储的缓存了 localStora ...

  2. 【转载】java中的反射

    主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...

  3. vue-cli index.js dev 配置中 assetsPublicPath 的值不能填 "./" 的问题

    问题 使用nginx又代理了一层 在浏览器中 / 代表域名的根目录,./代表当前路径 线上发布的时候一般都会使用nginx反向代理,所以使用./是最靠谱的,但是vue-cli dev 中的 asset ...

  4. python-selenium,关于页面滑动

    第一种: #滑到底部 js="var q=document.documentElement.scrollTop=100000" driver.execut_script(js) 目 ...

  5. 【剑指Offer】42、和为S的两个数字

      题目描述:   输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的.   输出描述:   对应每个测试案例,输出两个数, ...

  6. 基于 Nginx XSendfile + SpringMVC 进行文件下载

    转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...

  7. namespace、struct、enum、union、string(day01)

    一 C++概述 C++历史背景 )C++的江湖地位 jave C C++ C# python )C++之父:Bjarne Stroustrup(--) ,Cpre,为C语言增加类的机制 ,Bjarne ...

  8. Linux 系统查看命令

    1. linux CPU大小cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep &quo ...

  9. php 后端实现JWT认证方法

    JWT是什么 JWT是json web token缩写.它将用户信息加密到token里,服务器不保存任何用户信息.服务器通过使用保存的密钥验证token的正确性,只要正确即通过验证.基于token的身 ...

  10. Leetcode 887 Super Egg Drop(扔鸡蛋) DP

    这是经典的扔鸡蛋的题目. 同事说以前在uva上见过,不过是扔气球.题意如下: 题意: 你有K个鸡蛋,在一栋N层高的建筑上,被要求测试鸡蛋最少在哪一层正好被摔坏. 你只能用没摔坏的鸡蛋测试.如果一个鸡蛋 ...