学习笔记——原型模式Prototype
原型模式,简单说就是具有一个克隆方法,外部可以直接使用此方法得到相应对象的拷贝对象。

比如哆啦A梦的复制镜,一照,就把物品拷贝了一份(虽然是镜子复制是相反的,这里就忽略这个细节了)
C++中依靠拷贝构造函数来得到拷贝。
IPrototype* CPrototype::Clone() const
{
return new CPrototype(*this);
}
C#中浅拷贝依靠MemberwiseClone来实现,深拷贝需要自己实现,依靠 [Serializable]、MemoryStream或者简单的new新对象,然后复制值。
public CPrototype Clone()
{
return this.MemberwiseClone() as CPrototype;
}
C++示例
IPrototype.h
#pragma once
class IPrototype
{
public:
IPrototype(void);
virtual ~IPrototype(void);
public:
virtual IPrototype* Clone() const = ;
};
IPrototype.cpp
#include "IPrototype.h" IPrototype::IPrototype(void)
{
} IPrototype::~IPrototype(void)
{
}
Prototype.h
#pragma once
#include "iprototype.h"
class CPrototype :
public IPrototype
{
public:
CPrototype(void);
CPrototype(const CPrototype& cp);
CPrototype operator =(const CPrototype& cp);
~CPrototype(void);
public:
IPrototype* Clone() const;
private:
int a;
};
Prototype.cpp
#include "Prototype.h" CPrototype::CPrototype(void)
{
} CPrototype::~CPrototype(void)
{
} CPrototype::CPrototype(const CPrototype& cp)
{
this->a = cp.a;
} CPrototype CPrototype::operator=(const CPrototype& cp)
{
this->a = cp.a;
return *this;
} IPrototype* CPrototype::Clone() const
{
return new CPrototype(*this);
}
main.cpp
#include "Prototype.h"
#include <iostream> int main()
{
IPrototype* p1 = new CPrototype();
IPrototype* p2 = p1->Clone();
return ;
}
学习笔记——原型模式Prototype的更多相关文章
- 设计模式学习心得<原型模式 Prototype >
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式是实现了一个原型接口,该接口用于创建当 ...
- 设计模式 笔记 原型模式 prototype
//---------------------------15/04/07---------------------------- //prototype 原型模式--对象创建型模式 /* 1:意图: ...
- C#学习笔记-原型模式
题目:编写基本的简历. 实现: 创建基本的Resume类,然后主函数通过实例化Resume来写简历即可. Resume类: class Resume { private string name; pr ...
- 设计模式学习之原型模式(Prototype,创建型模式)(5)
通过序列化的方式实现深拷贝 [Serializable] public class Person:ICloneable { public string Name { get; set; } publi ...
- 设计模式系列之原型模式(Prototype Pattern)——对象的克隆
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 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 ...
随机推荐
- digitalocean vpn安装配置教程
digitalocean是美国一家专业的vps提供商,优势是性价比高,最低配置512MB内存vps每月只要5美元,导致大陆用户疯狂涌入.关于digitalocean申请方法.digitalocean速 ...
- Openjudge-计算概论(A)-找和为K的两个元素
描述: 在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k. 输入第一行输入序列的长度n和k,用空格分开.第二行输入序列中的n个整数,用空格分开.输出如果存在某两个元 ...
- Openjudge-计算概论(A)-点与正方形的关系
描述: 有一个正方形,四个角的坐标(x,y)分别是(1,-1),(1,1),(-1,-1),(-1,1),x是横轴,y是纵轴.写一个程序,判断一个给定的点是否在这个正方形内.输入输入坐标x,y输出ye ...
- MathML转换成OfficeML
public XslCompiledTransform XslTransforms; XslTransforms = new XslCompiledTransform(); XslTransforms ...
- (╭ ̄3 ̄)╭ 小希的迷宫II
(╭ ̄3 ̄)╭ 小希的迷宫II TimeLimit: 2000/1000 MS (Java/Others) MenoryLimit: 65536/32768 K (Java/Others) 64-b ...
- Python笔记2-20151023
一.循环 Python的循环有两种,一种是for...in循环,依次吧list或tuple中的每个元素迭代出来. >>>names = ['Michael','Bob','Tracy ...
- List泛型集合常用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...
- oracle创建数据库表空间
1.创建表空间(存放数据) create tablespace xtba_datadatafile 'F:\ORACLE\ORADATA\ORCL\XTBA.DBF'size 50mautoexten ...
- PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件
作 为PHP入门开发者,常常有这种述求:自己的网站中需要添加一个使用自己的域名作为发件人邮件地址的自动发送邮件的方法,用于诸如给用户发送验证码.通知 信息等.比如:我的某个用户注册模块,需要使用reg ...
- gridControl 中CellValueChanged,ShowingEditor,CustomDrawCell的用法
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventA ...