原型模式(Prototype)C++实现
意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
实用性: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;
}
转载于:https://www.cnblogs.com/wrbxdj/p/4171483.html
原型模式(Prototype)C++实现相关推荐
- 原型模式(ProtoType) - Java里的对象复制
一, 引用的复制和对象复制. 在编程中, 我们有时会用两个引用指向同一个对象. 例如: ArrayList a = new ArrayLIst(); ArrayList b = a; 看起来好像有a, ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
[索引页] [源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:webabcd 介绍 用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象. ...
- 原型模式-prototype
一.什么是原型模式 Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象的实例.使用Prototype模式创建的实例,具有与原型一样的数据. 二.原型模式的特点 1.由原型对 ...
- C++设计模式——原型模式(Prototype Pattern)
C++设计模式--原型模式(Prototype Pattern) 微信公众号:幼儿园的学霸 目录 文章目录 C++设计模式--原型模式(Prototype Pattern) 目录 定义 代码示例 普通 ...
- 原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. ...
- Java设计模式--原型模式Prototype
原型模式Prototype 原型模式使得用户可以通过复制对象样本来创建新对象.与通过调用构造函数创建对象相比,二者主要区别在于:通过复制创建的新对象一般会包含原始对象的某些状态. 原型模式属于对象的创 ...
- 设计模式(23):创建型-原型模式(Prototype)
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...
- 设计模式之原型模式(Prototype)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式包括:1.FactoryMethod(工厂方法模式):2.Abstract Factory(抽象工厂模式):3.Sin ...
- 设计模式之原型模式prototype
1.原型模式的使用和本质.以及优势: a.通过 new 产生一个对象需要非常繁琐的数据准备或者访问权限,则可以使用原型模式. b.原型模式的使用就是 java 中的克隆技术,以某个对象为原型,复制出新 ...
- 设计模式---原型模式(Prototype Pattern)
在编程中有时候我们会发现,当我们需要一个实例,可是这个实例的创建过程十分复杂,在执行过程中 会消耗大量的时间,同时创建第一个实例和创建第二个时间的初始化信息并未改变.在此种情况下,直接New 一个实例 ...
最新文章
- java键盘输入运算符_Java基础学习-三元运算符和键盘录入的基本步骤和使用
- 如何将struct System.Byte byte []转换为C#中的System.IO.Stream对象?
- 用pytorch及numpy计算成对余弦相似性矩阵,并用numpy实现kmeans聚类
- 关于vs2012、tfs2012、windows server 2008r2一些记录
- 你知道undefined与null的区别吗?
- 入侵韩国某购物网并提权
- 四、Hyper-v Server 2008r2 设置远程管理
- 假如时光能够倒流, 我会这么学习Java
- PHP案例-精彩商城教学
- CMUX协议学习总结
- 零食社交 or 甜蜜陷阱?说说公司那些免费提供的零食饮料
- 基于JMF RTP的音视频传输
- deb 中标麒麟_麒麟动态·亿图软件登陆银河麒麟桌面三部曲之——项目管理软件 - 银河麒麟操作系统 麒麟操作系统 中标麒麟 麒麟软件官方网站...
- 数据结构化和半结构化的区别
- 高通败诉,授权专利技术之后高通的优势不再?
- 智真长老临别四句偈言 智深圆寂偈语
- html 隐藏表格某一行,layui怎么隐藏表格行?
- html段落间距怎么缩小,WPS怎样缩小段落间距
- 如何将应用隐藏成一个计算机,局域网中如何隐藏自己的计算机
- insurgency服务器修改,insurgency指令大全 | 手游网游页游攻略大全