c++原型模式(Prototype)
原型模式是通过已经存在的对象的接口快速方便的创建新的对象。
#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)的更多相关文章
- 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 ...
随机推荐
- uboot Makefile 文件源码分析
Makefile 是一个神奇的文件 详情参考uboot配置和编译过程详解
- 【JavsScript高级程序设计】学习笔记[1]
1. 在HTML中使用javascript 在使用script嵌入脚本时,脚本内容不能包含'</script>'字符串(会被认为是结束的标志).可以通过转义字符解决('\') 默认scri ...
- netcat 瑞士军刀
netcat被誉为网络安全界的‘瑞士军刀’,一个简单而有用的工具,透过使用TCP或UDP协议的网络连接去读写数据.它被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强 ...
- 安卓开发第一记 android stdio 安装后 新建测试项目报错
Failed to resolve:com.android.support:appcompat-v7:报错处理 你在使用android studio时是否也出现过上图的报错,你还在为它的出现烦恼? ...
- ASP.NET中服务器控件的生命周期
服务器控件的生命周期是创建服务器控件最重要的概念.作为开发人员,必须对服务器控件生命周期深刻理解.当然,这不是一朝一夕就可以做到的.对于学习控件开发技术的初学者,可以不必掌握得非常详细深入,只需对服务 ...
- 【java规则引擎】drools6.5.0中kie的概论
什么是KIE? KIE是jBoss里面一些相关项目的统称,下图就是KIE代表的一些项目,其中我们比较熟悉的就有jBPM和Drools. 这些项目都有一定的关联关系,并且存在一些通用的API,比如说涉及 ...
- gulp 流处理
包含的内容: 穿插流 流合并 流队列 流筛选 1. 穿插流 // passthrough stream 把流传递给其他的 // use gulp.src(glob,{p ...
- Note: log switch off, only log_main and log_events will have logs!
真机(华为c8813)在Eclipase上测试,打不出logcat信息,只有这样的一句话:Note: log switch off, only log_main and log_events will ...
- php end()
end()的用法
- [转载]交换机STP协议
注:之前做一个项目,测试部使用2个公司的交换机,H3C和H公司的,H公司的交换机是OEM H3C的交换机,正常来说两者使用没有区别. 但是使用中发现,如果设备的多个对外业务网口连接的交换机的聚合网口, ...