原型模式(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;
}
原型模式(Prototype)C++实现的更多相关文章
- Net设计模式实例之原型模式( Prototype Pattern)
一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...
- 二十四种设计模式:原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...
- 设计模式(四)原型模式Prototype(创建型)
设计模式(四)原型模式Prototype(创建型) 1. 概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- 原型模式-Prototype(Java实现)
原型模式-Prototype 通过复制(克隆.拷贝)一个指定类型的对象来创建更多同类型的对象. 就像去蛋糕店买蛋糕一样. 柜台里的蛋糕都是非卖品. 只是为顾客提供一种参照. 当顾客看上某一个样式的蛋糕 ...
- 原型模式 prototype 创建型 设计模式(七)
原型模式 prototype 意图 用原型实例指定需要创建的对象的类型,然后使用复制这个原型对象的方法创建出更多同类型的对象 显然,原型模式就是给出一个对象,然后克隆一个或者更多个对象 小时候看 ...
- PHP设计模式 原型模式(Prototype)
定义 和工厂模式类似,用来创建对象.但实现机制不同,原型模式是先创建一个对象,采用clone的方式进行新对象的创建. 场景 大对象的创建. 优点 1.可以在运行时刻增加和删除产品 2.可以改变值或结构 ...
- 设计模式系列之原型模式(Prototype Pattern)——对象的克隆
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 六个创建模式之原型模式(Prototype Pattern)
定义: 使用原型实例指定创建对象的种类,并通过拷贝这个原型的属性创建新的对象. 结构图: Prototype:抽象原型类,声明克隆方法的接口,并是所有原型类的公共父类.在Java中,Object类为该 ...
- [工作中的设计模式]原型模式prototype
一.模式解析 提起prototype,最近看多了js相关的内容,第一印象首先是js的原型 var Person=function(name){ this.name=name; } Person.pro ...
随机推荐
- 09--c++ 类的继承与派生
c++ 类的继承与派生 一.基本概念 1.类的继承,是新的类从已有类那里得到已有的特性.或从已有类产生新类的过程就是类的派生.原有的类称为基类或父类,产生的新类称为派生类或子类. 2.派生类的 ...
- HDU_3999_二叉排序树
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- BZOJ 1572: [Usaco2009 Open]工作安排Job 贪心 + 堆 + 反悔
Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...
- github+hexo(window10)
一.申请github账户 二.先安装node.js.git 本地: 三.安装hexo(建立静态网页,用Markdown写博客) 1.创建文件地址 在合适的地方新建一个文件夹,用来存放自己的博客文件,比 ...
- 11.5 【Linq 】连接
11.5.1 使用 join 子句的内连接 如果你打算把一个巨大的序列连接到一个极小的序列上,应尽可能把小序列作为右边序列 class Program { static void Main(strin ...
- 将RedHat的yum更换为CentOS的yum
CentOS6.8 脚本: #安装yum所需的包已经下载到本地 #!/bin/bashrpm -qa | grep yum | xargs rpm -e --nodepsrm -rf /etc/yum ...
- 08.Web服务器-5.深入理解HTTP协议(大图)
- python爬虫03:那个叫做 Urllib 的库让我们的 python 假装是浏览器
相信你已经摸清了 浏览器各种请求的套路 也知道了怎么在手机上进行请求和返回数据的抓取 那么接下来我们就开始来使用 python 了 代码 lu 起来 那么 怎么用 python 写各种请求呢? 今天要 ...
- Git 基础教程 之 --no-ff模式合并
① 创建并切换dev分支 ② 修改readme.txt,并add,commit ③ 切回master ④ 合并 git merge --no-ff -m “merge with no-ff”d ...
- ThinkPHP5.0 模型查询操作
1.获取单个数据 //取出主键为1的数据 $user = User::get(1); echo $user->name; // 使用数组查询 $user = User::get(['name' ...