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

实用性: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. php 加密解密函数封装

    算法一: //加密函数 function lock_url($txt,$key='yang') { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi ...

  2. rabbitmq-3.5.1-安裝

    系统版本:CentOS 6.5RabbitMQ-Server:3.5.1一.安装erlang1.安装准备,下载安装文件 wget https://packages.erlang-solutions.c ...

  3. PAT_A1118#Birds in Forest

    Source: PAT A1118 Birds in Forest (25 分) Description: Some scientists took pictures of thousands of ...

  4. codeforces 427D Match & Catch(后缀数组,字符串)

    题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等 ...

  5. 【codeforces 797D】Broken BST

    [题目链接]:http://codeforces.com/contest/797/problem/D [题意] 给你一个二叉树; 然后问你,对于二叉树中每个节点的权值; 如果尝试用BST的方法去找; ...

  6. jquery-cookie持久化

    jQuery.Cookie.js,是一个轻量级的Cookie管理插件 <script src="jQuery.1.8.3.js" type="text/javasc ...

  7. RONOJ——PID204 / 特种部队 ☆

    题目描述 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面 牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库. 当他们到达仓库时候,发现这个仓库的锁是一把很 ...

  8. Linux排序命令sort(转)

    Linux sort命令用于将文本文件内容加以排序.sort可针对文本文件的内容,以行为单位来排序. 语法 sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符&g ...

  9. 《coredump问题原理探究》Linux x86版7.7节 set对象

    看一下bits/stl_map和bits/stl_set能够看到map和set的定义例如以下: 84 template <typename _Key, typename _Tp, typenam ...

  10. ABCDE

    ABCDE A-Artificial intelligence 人工智能 B-Block chain 区块链 C-Cloud 云 D-Big Data 大数据 E-Ecology 互联网生态是以互联网 ...