1、原型模式简介

1.1>、定义

  原型模式(Prototype)用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。

1.2>、使用频率

  

1.3>、原型模式应用

  首先从实际生活来了解原型模式的由来,假设你有一份非常好的讲义,你的朋友也想要一份,那么怎么办?重新手抄一份?显然不是,当然是用复印机复印一份来得方便、直接,并且准确性也高,这种用原型来复制而不是重新创建的思维方式就是原型模式的核心思想。

  Prototype Pattern也是一种创建型模式,它关注的是大量相同或相似对象的创建问题。应用原型模式就是建立一个原型,然后通过对原型来进行复制的方法,来产生一个和原型相同或相似的新对象,或者说用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。

2、原型模式结构

2.1>、结构图

2.2>、参与者

  原型模式参与者:

  ◊ Prototype:原型类,声明一个Clone自身的接口;

  ◊ ConcretePrototype:具体原型类,实现一个Clone自身的操作。

  在原型模式中,Prototype通常提供一个包含Clone方法的接口,具体的原型ConcretePrototype使用Clone方法完成对象的创建。

3、原型模式结构实现

  Prototype.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DesignPatterns.PrototypePattern.Structural
  7. {
  8. /// <summary>
  9. /// The 'Prototype' abstract class
  10. /// </summary>
  11. public abstract class Prototype
  12. {
  13. private string _id;
  14.  
  15. /// <summary>
  16. /// Constructor
  17. /// </summary>
  18. public Prototype(string id)
  19. {
  20. this._id = id;
  21. }
  22.  
  23. /// <summary>
  24. /// Gets id
  25. /// </summary>
  26. public string Id
  27. {
  28. get { return _id; }
  29. }
  30.  
  31. public abstract Prototype Clone();
  32. }
  33. }

  ConcretePrototype1.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DesignPatterns.PrototypePattern.Structural
  7. {
  8. public class ConcretePrototype1 : Prototype
  9. {
  10. /// <summary>
  11. /// Constructor
  12. /// </summary>
  13. public ConcretePrototype1(string id)
  14. : base(id)
  15. {
  16. }
  17.  
  18. /// <summary>
  19. /// Returns a shallow copy
  20. /// </summary>
  21. /// <returns></returns>
  22. public override Prototype Clone()
  23. {
  24. return (Prototype)this.MemberwiseClone();
  25. }
  26. }
  27. }

  ConcretePrototype2.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DesignPatterns.PrototypePattern.Structural
  7. {
  8. public class ConcretePrototype2 : Prototype
  9. {
  10. /// <summary>
  11. /// Constructor
  12. /// </summary>
  13. public ConcretePrototype2(string id)
  14. : base(id)
  15. {
  16. }
  17.  
  18. /// <summary>
  19. /// Returns a shallow copy
  20. /// </summary>
  21. /// <returns></returns>
  22. public override Prototype Clone()
  23. {
  24. return (Prototype)this.MemberwiseClone();
  25. }
  26. }
  27. }

  Client.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using DesignPatterns.PrototypePattern.Structural;
  7.  
  8. namespace DesignPatterns.PrototypePattern
  9. {
  10. class Client
  11. {
  12. static void Main(string[] args)
  13. {
  14. // Create two instances and clone each
  15. ConcretePrototype1 p1 = new ConcretePrototype1("I");
  16. ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
  17. Console.WriteLine("Cloned: {0}", c1.Id);
  18.  
  19. ConcretePrototype2 p2 = new ConcretePrototype2("II");
  20. ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
  21. Console.WriteLine("Cloned: {0}", c2.Id);
  22. }
  23. }
  24. }

  运行输出:

  1. Cloned: I
  2. Cloned: II
  3. 请按任意键继续. . .

4、原型模式实践应用

  ColorPrototype.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DesignPatterns.PrototypePattern.Practical
  7. {
  8. /// <summary>
  9. /// The 'Prototype' abstract class
  10. /// </summary>
  11. public abstract class ColorPrototype
  12. {
  13. public abstract ColorPrototype Clone();
  14. }
  15. }

  Color.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DesignPatterns.PrototypePattern.Practical
  7. {
  8. /// <summary>
  9. /// The 'ConcretePrototype' class
  10. /// </summary>
  11. public class Color : ColorPrototype
  12. {
  13. private int _red;
  14. private int _green;
  15. private int _blue;
  16.  
  17. /// <summary>
  18. /// Constructor
  19. /// </summary>
  20. public Color(int red, int green, int blue)
  21. {
  22. this._red = red;
  23. this._green = green;
  24. this._blue = blue;
  25. }
  26.  
  27. /// <summary>
  28. /// Create a shallow copy
  29. /// </summary>
  30. public override ColorPrototype Clone()
  31. {
  32. Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);
  33. return this.MemberwiseClone() as ColorPrototype;
  34. }
  35. }
  36. }

  ColorManager.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DesignPatterns.PrototypePattern.Practical
  7. {
  8. /// <summary>
  9. /// Prototype manager
  10. /// </summary>
  11. public class ColorManager
  12. {
  13. private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>();
  14.  
  15. /// <summary>
  16. /// Indexer
  17. /// </summary>
  18. public ColorPrototype this[string key]
  19. {
  20. get { return _colors[key]; }
  21. set { _colors.Add(key, value); }
  22. }
  23. }
  24. }

  Client.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using DesignPatterns.PrototypePattern.Practical;
  7.  
  8. namespace DesignPatterns.PrototypePattern
  9. {
  10. class Client
  11. {
  12. static void Main(string[] args)
  13. {
  14. ColorManager colormanager = new ColorManager();
  15.  
  16. // Initialize with standard colors
  17. colormanager["red"] = new Color(, , );
  18. colormanager["green"] = new Color(, , );
  19. colormanager["blue"] = new Color(, , );
  20.  
  21. // User adds personalized colors
  22. colormanager["angry"] = new Color(, , );
  23. colormanager["peace"] = new Color(, , );
  24. colormanager["flame"] = new Color(, , );
  25.  
  26. // User clones selected colors
  27. Color color1 = colormanager["red"].Clone() as Color;
  28. Color color2 = colormanager["peace"].Clone() as Color;
  29. Color color3 = colormanager["flame"].Clone() as Color;
  30. }
  31. }
  32. }

  运行输出:

  1. Cloning color RGB: , ,
  2. Cloning color RGB: ,,
  3. Cloning color RGB: , ,
  4. 请按任意键继续. . .

5、原型模式应用分析

  原型模式可以适用于以下情形:

  ◊ 当一个系统应该独立于它的产品创建、构成和表示时;

  ◊ 当要实例化的类是在运行时刻指定时,例如通过动态装载来创建一个类;

  ◊ 为了避免创建一个与产品类层次平行的工厂类层次时;

  ◊ 当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并Clone它们可能比每次用合适的状态手工实例化该类更方便一些。

  原型模式具有以下特点:

  ◊ 对客户隐藏了具体的产品类,因此减少了客户知道的名字的数目;

  ◊ 允许客户只通过注册原型实例就可以将一个具体产品类并入到系统中,客户可以在运行时刻建立和删除原型;

  ◊ 减少了子类的构造。原型模式是Clone一个原型而不是请求工厂方法创建一个,所以它不需要一个与具体产品类平行的Creator类层次;

  ◊ 原型模式具有给一个应用软件动态加载新功能的能力。由于Prototype的独立性较高,可以很容易动态加载新功能而不影响旧系统;

  ◊ 产品类不需要非得有任何事先确定的等级结构,因为原型模式适用于任何的等级结构;

  ◊ 原型模式的最重要缺点就是每一个类必须配备一个Clone方法,而且这个Clone方法需要对类的功能进行通盘考虑。这对全新的类来说不是很难,但对已有的类进行改造时,不一定是容易的事。

C#设计模式系列:原型模式(Prototype)的更多相关文章

  1. 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

    原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...

  2. 二十四种设计模式:原型模式(Prototype Pattern)

    原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...

  3. [设计模式] 4 原型模式 prototype

    设计模式:可复用面向对象软件的基础>(DP)本文介绍原型模式和模板方法模式的实现.首先介绍原型模式,然后引出模板方法模式. DP书上的定义为:用原型实例指定创建对象的种类,并且通过拷贝这些原型创 ...

  4. 设计模式 笔记 原型模式 prototype

    //---------------------------15/04/07---------------------------- //prototype 原型模式--对象创建型模式 /* 1:意图: ...

  5. python 设计模式之原型模式 Prototype Pattern

    #引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...

  6. 【UE4 设计模式】原型模式 Prototype Pattern

    概述 描述 使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.如孙悟空猴毛分身.鸣人影之分身.剑光分化.无限剑制 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, ...

  7. 【设计模式】—— 原型模式Prototype

    前言:[模式总览]——————————by xingoo 模式意图 由于有些时候,需要在运行时指定对象时哪个类的实例,此时用工厂模式就有些力不从心了.通过原型模式就可以通过拷贝函数clone一个原有的 ...

  8. 创建型设计模式之原型模式(Prototype)

    结构   意图 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 适用性 当要实例化的类是在运行时刻指定时,例如,通过动态装载:或者 为了避免创建一个与产品类层次平行的工厂类层次时:或 ...

  9. Android设计模式系列--原型模式

    CV一族,应该很容易理解原型模式的原理,复制,粘贴完后看具体情况是否修改,其实这就是原型模式.从java的角度看,一般使用原型模式有个明显的特点,就是实现cloneable的clone()方法.原型模 ...

  10. 设计模式五: 原型模式(Prototype)

    简介 原型模式是属于创建型模式的一种,是通过拷贝原型对象来创建新的对象. 万能的Java超类Object提供了clone()方法来实现对象的拷贝. 可以在以下场景中使用原型模式: 构造函数创建对象成本 ...

随机推荐

  1. 疑难问题解决备忘录(1)——LAMP环境下WordPress无法发现themes目录下的主题问题解决

    程序猿的宿命就是无穷无尽地解题,虽然可以说是解题的机器,但也无法达到解题之神的境界,碰到自己解决不了的问题那是家常便饭,尤其当遍寻Google和StackOverflow花了九牛二虎之力才解决的问题, ...

  2. 子代选择器(>)后代选择器(' ')的区别

    子代选择器是指紧接着父级的那个标签,如:container>a指的是紧接着container后面的第一个a(儿子级别的,孙子或者之后的a是不能生效的) 后代选择器是用空格分开的,如:contai ...

  3. 2分钟 sublime设置自动行尾添加分号并换行:

    18:03 2016/4/162分钟 sublime设置自动行尾添加分号并换行:注意:宏文件路径要用反斜杠/,2个\\会提示无法打开宏文件.不需要绝对路径很简单利用宏定义:1.录制宏:由于是录制动作宏 ...

  4. web程序的路径笔记

    "/"与”\“区别:”/“是unix系统区分文件层级的标志,因为当前web应用程序在服务器端大都使用基于unix系统开发的操作系统,所以web程序包括浏览器里url都遵以”/“来区 ...

  5. jquery修改Switchery复选框的状态

    script //选择框 var mySwitch; /* * 初始化Switchery * * classNmae class名 */ function initSwitchery(classNam ...

  6. SQL Server

    1.通过触发器来级联删除: 具体的触发器代码如下: Create TRIGGER [dbo].[DeleteRelatedProducts] ON [dbo].[ProductCategory]  A ...

  7. 正确获取访问者ip

    使用$_SERVER['REMOTE_ADDR']获取访问者ip具有局限性.比如访问者系统位于docker环境时,$_SERVER['REMOTE_ADDR']获取到的ip为虚拟ip,而不是我们真正需 ...

  8. CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

    安装篇: 一.安装Apache yum install httpd #根据提示,输入Y安装即可成功安装 /etc/init.d/httpd start#启动Apache 备注:Apache启动之后会提 ...

  9. 第三方框架之ThinkAndroid 学习总结(二)

    上文记录了一些ThinkAndroid常用的模块,本文继续介绍ThinkAndroid中的网络化模块. 按照惯例先上Github原文地址:https://github.com/white-cat/Th ...

  10. mysql解决其他服务器不可连接问题

    在安装mysql的机器上运行: 1.d:\mysql\bin\>mysql   -h   localhost   -u   root //这样应该可以进入MySQL服务器 2.mysql> ...