原型模式是通过已经存在的对象的接口快速方便的创建新的对象。

#include <iostream>
#include <string> using namespace std; class Prototype
{
public:
virtual Prototype *Clone() = ;
virtual void display() = ;
}; class Prototype1:public Prototype
{
protected:
string name;
int id;
public:
Prototype1(string name, int id)
{
this->name = name;
this->id = id;
}
Prototype1(const Prototype1 &type)
{
this->name = type.name;
this->id = type.id;
}
virtual void display()
{
cout<< "my name and id are :" << this->id << " "<<this->name <<endl;
} Prototype *Clone()
{
return new Prototype1(*this);
} };
class Prototype2:public Prototype
{
protected:
string name;
public:
Prototype2(string name)
{
this->name = name;
}
Prototype2(const Prototype2 &type)
{
this->name = type.name;
}
virtual void display()
{
cout << "my name is :" << this->name << endl;
}
Prototype *Clone()
{
return new Prototype2(*this);
} }; int main()
{
Prototype *obj1 = new Prototype1("mafuli",);
Prototype *obj2 = obj1->Clone();
Prototype *obj3 = obj2->Clone(); obj2->display();
obj3->display(); Prototype *obj4 = new Prototype2("cxue");
Prototype *obj5 = obj4->Clone();
Prototype *obj6 = obj5->Clone(); obj4->display();
obj5->display();
obj6->display(); getchar();
return ;
}

输出信息为:

my name and id are : mafuli
my name and id are : mafuli
my name is :cxue
my name is :cxue
my name is :cxue

c++原型模式(Prototype)的更多相关文章

  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. Kali Linux:使用nmap扫描主机

    nmap-Network Mapper,是著名的网络扫描和嗅探工具包.他同样支持Windows和OS X. 扫描开放端口和判断操作系统类型 先让我们ping一段地址范围,找到启动的主机: # nmap ...

  2. 前端之jQuery03 插件

    jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法(通常用来制作插件) 增加两个插件方法: // jQuery 扩展机制 // 自己扩展两个方法 // 把我这个 ...

  3. 解决跨域POST登录中IE不能正常工作的bug

    结合我的这篇blog <简单实用的跨域表单POST提交> 文章,这篇blog中的思路是解决在www.a.com站中登录 同时要把关联站www.b.com登录状态也设置成登录状态,在a中获取 ...

  4. WCF *.svc 自定义地址路由映射

    一般在创建WCF服务时会用Serivce.svc文件访问,地址如:http://localhost/applicationname/Serivce.svc/Name 现在用路由映射成:http://l ...

  5. WCF OpenTimeout, CloseTimeout, SendTimeout, ReceiveTimeout

    1.OpenTimeout 客户端与服务端建立连接时,如果超过指定时间都还没完成,就引发TimeoutException. 在TCP通讯中,服务器必须首先准备好侦听端口并在该端口上侦听(Listen) ...

  6. LeetCode 616. Add Bold Tag in String

    原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...

  7. LG2120 [ZJOI2007]仓库建设

    题意 L公司有N个工厂,由高到底分布在一座山上. 工厂1在山顶,工厂N在山脚. 由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用. 突然有一天,L公司的总裁L先生接到 ...

  8. yii 操作cookie

    原文地址:http://blog.sina.com.cn/s/blog_664c9f650100yqkn.html 设置cookie: $cookie = new CHttpCookie('mycoo ...

  9. 使用.NET Remoting开发分布式应用——基于租约的生存期

    一.概述 知名类型的SingleCall对象可以在客户程序的方法调用之后被垃圾收集器清理掉,因为它没有保持状态,属于无状态的.而客户激活的类型的对象和知名类型的SingleTon对象都属于生存期长的对 ...

  10. IE9以上版本无法更換会员头像

    解决方法一:把网址加入"信任的網站" 就可以了 解决方法二:打开IE8的窗口-工具-- Intelnet选项-- 安全-- 自定义级别把“将文件上传到服务器时包含本地目录路径”这一 ...