23种设计模式之原型模式(Prototype Pattern)
原型模式
使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象
分析:
/// <summary>
/// 原型模式:单例的基础上升级了一下,把对象从内存层面复制了一下,然后返回
/// 是个新对象,但是又不是new出来的
/// </summary>
[Serializable]
public class Prototype
{
/// <summary>
/// 构造函数耗时耗资源
/// </summary>
private Prototype()
{
long lResult = ;
for (int i = ; i < ; i++)
{
lResult += i;
}
Thread.Sleep();
Console.WriteLine("{0}被构造一次", this.GetType().Name);
}
/// <summary>
/// 3 全局唯一静态 重用这个变量
/// </summary>
private static volatile Prototype _Prototype = new Prototype()
{
CurrentEmployee = "admin",
Member = new Member { Id=,Name="aaa"}
};
/// <summary>
/// 2 公开的静态方法提供对象实例
/// </summary>
/// <returns></returns>
public static Prototype CreateInstance()
{
Prototype prototype = (Prototype)_Prototype.MemberwiseClone();
return prototype;
}
public static Prototype DeepClone()
{
Prototype prototype = JsonConvert.DeserializeObject<Prototype>(JsonConvert.SerializeObject(_Prototype));
return prototype;
}
public Member Member { get; set; }
public string CurrentEmployee { get; set; }
}
[Serializable]
public class Member
{
public int Id { get; set; }
public string Name { get; set; }
}
前端调用
{//浅拷贝
Prototype prototype1 = Prototype.CreateInstance();
Prototype prototype2 = Prototype.CreateInstance();
prototype1.CurrentEmployee = "prototype1";
prototype1.Member.Id = ;
prototype1.Member.Name = "bbb";
prototype2.CurrentEmployee = "prototype2";
prototype2.Member.Id = ;
prototype2.Member.Name = "ccc";
Console.WriteLine(prototype1.CurrentEmployee);
Console.WriteLine(prototype1.Member.Id);
Console.WriteLine(prototype1.Member.Name);
Console.WriteLine(prototype2.CurrentEmployee);
Console.WriteLine(prototype2.Member.Id);
Console.WriteLine(prototype2.Member.Name);
}
{//深拷贝
Prototype prototype1 = Prototype.DeepClone();
Prototype prototype2 = Prototype.DeepClone();
prototype1.CurrentEmployee = "prototype1";
prototype1.Member.Id = ;
prototype1.Member.Name = "bbb";
prototype2.CurrentEmployee = "prototype2";
prototype2.Member.Id = ;
prototype2.Member.Name = "ccc";
Console.WriteLine(prototype1.CurrentEmployee);
Console.WriteLine(prototype1.Member.Id);
Console.WriteLine(prototype1.Member.Name);
Console.WriteLine(prototype2.CurrentEmployee);
Console.WriteLine(prototype2.Member.Id);
Console.WriteLine(prototype2.Member.Name);
}
执行结果

深度拷贝就是利用序列化与反序列化创建对象,这种方法好像也走构造函数,那么还不如直接new 就失去了原型模式的意义了
23种设计模式之原型模式(Prototype Pattern)的更多相关文章
- 二十四种设计模式:原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- 23种设计模式之原型模式(Prototype)
在系统开发过程中,有时候有些对象需要被频繁创建,原型模式通过给出一个原型对象来指明所要创建的对象的类型,然后通过复制这个原型对象的办法,创建出更多同类型的对象.原型模式是一种对象创建型模式,用原型实例 ...
- 【Unity与23种设计模式】原型模式(Prototype)
GoF中定义: "使用原型对象来产生指定类的对象,所以产生对象时,是使用复制原型对象来完成." Unity中 开发者可以组装游戏对象 它可以包括复杂的组件 组装好了之后,就可以将其 ...
- python 设计模式之原型模式 Prototype Pattern
#引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...
- 【UE4 设计模式】原型模式 Prototype Pattern
概述 描述 使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.如孙悟空猴毛分身.鸣人影之分身.剑光分化.无限剑制 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, ...
- 23种设计模式--中介者模式-Mediator Pattern
一.中介者模式的介绍 中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...
- Net设计模式实例之原型模式( Prototype Pattern)
一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...
- 设计模式系列之原型模式(Prototype Pattern)——对象的克隆
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
随机推荐
- Scala 系列(十二)—— 类型参数
一.泛型 Scala 支持类型参数化,使得我们能够编写泛型程序. 1.1 泛型类 Java 中使用 <> 符号来包含定义的类型参数,Scala 则使用 []. class Pair[T, ...
- FEL,项目实装记录
FEL,即Fast EL ,版本0.8,具体内容我就不贴了,自行百度 实装遇到的问题: Spring Boot 打包后无法进行表达式编译. 根据百度以及源码,确定这东西是在用JavaCompiler将 ...
- CF #535 (Div. 3) E2 Array and Segments (Hard version) 利用线段树进行区间转移
传送门 题意: 有m个区间,n个a[ i ] , 选择若干个区间,使得整个数组中的最大值和最小值的差值最小.n<=1e5,m<=300; 思路: 可以知道每个i,如果一个区间包含这个 ...
- HDU2222Keywords Search AC_自动机
http://blog.csdn.net/niushuai666/article/details/7002823 #include <iostream> #include <cstd ...
- 杭电第六场 hdu6362 oval-and-rectangle 积分求期望
oval-and-rectangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- CodeForces 760 C. Pavel and barbecue(dfs+思维)
题目链接:http://codeforces.com/contest/760/problem/C 题意:一共有N个烤炉,有N个烤串,一开始正面朝上放在N个位子上.一秒之后,在位子i的串串会移动到pi位 ...
- CF993A Two Squares 几何 第二道 暴力或判断条件(*)
Two Squares time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- poj 2352 & Ural 1028 数星星 题解
一道水题,由于x坐标递增y坐标也递增于是前缀和统计即可,用树状数组实现. #include<bits/stdc++.h> using namespace std; const int ma ...
- webpack4 output配置 filename chunkhash报错
这里的hash由chunkhash改成hash,原因是使用HotModuleReplacementPlugin之后不能使用chunkhash和contenthash.看到有些地方说把“hot:true ...
- 用.NET写“算命”程序
用.NET写"算命"程序 "算命",是一种迷信,我父亲那一辈却执迷不悟,有时深陷其中,有时为求一"上上签",甚至不惜重金,向"天神 ...