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 ...
随机推荐
- hexo搭建个人主页托管于github
之前学习了 如何利用Github免费搭建个人主页,今天利用hexo来快速生成个人网页托管于github上. hexo系列教程:(一)hexo介绍 什么是hexo hexo是一个基于Node.js的静态 ...
- niosii boot过程
1 概述Nios II 的boot过程要经历两个过程. FPGA器件本身的配置过程.FPGA器件在外部配置控制器或自身携带的配置控制器的控制下配置FPGA的内部逻辑.如果内部逻辑中使用了Nios II ...
- fn project 对象模型
Applications At the root of everything are applications. In fn, an application is essentially a grou ...
- C#防止程序多次运行
经过我的测试,还比较好用,但是有个问题,如果不注销,用另一个用户进入,则程序不能判断出已运行.所以只限于用在单用户环境,还是不太完美. class Program { [STAThread] stat ...
- 区域存储网络(SAN)与 网络直接存储(NAS)
随着互联网及网络应用的飞速发展,数据信息存储系统所需处理的数据类型也呈爆炸性增长,这使数据信息存储系统面临前所未有的挑战.附加式网络存储装置(Network Attached Storage,缩写为N ...
- YII框架安装过程-数据库访问
1.电脑上原来安装了phpstudy.关掉phpstudy,启动wamp,虽启动成功,但仍然无法使用phpmyadmin登录数据库管理页面. 2.查看到系统服务有mysql服务,检查属性均为emsoa ...
- bzoj 2159 Crash 的文明世界 && hdu 4625 JZPTREE ——第二类斯特林数+树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 学习材料:https://blog.csdn.net/litble/article/d ...
- [转]json-lib 的maven dependency
转载自http://www.cnblogs.com/yqskj/archive/2013/05/27/3101934.html 项目中要用到json-lib,mvnrepository.com查找它的 ...
- JVM知识整理和学习(转载并修改)
JVM是虚拟机,也是一种规范,他遵循着冯·诺依曼体系结构的设计原理. 冯·诺依曼体系结构中,指出计算机处理的数据和指令都是二进制数,采用存储程序方式不加区分的存储在同一个存储器里,并且顺序执行,指令由 ...
- TELNET协议规范
ARPA Internet上的主机被要求采用并实现此标准. 介绍 TELNET Protocol的目的是提供一个相对通用的,双向的,面向八位字节的通信方法.它主要的目标是允许接口终端设备的标准方法和面 ...