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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.PrototypePattern.Structural
{
/// <summary>
/// The 'Prototype' abstract class
/// </summary>
public abstract class Prototype
{
private string _id; /// <summary>
/// Constructor
/// </summary>
public Prototype(string id)
{
this._id = id;
} /// <summary>
/// Gets id
/// </summary>
public string Id
{
get { return _id; }
} public abstract Prototype Clone();
}
}

  ConcretePrototype1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.PrototypePattern.Structural
{
public class ConcretePrototype1 : Prototype
{
/// <summary>
/// Constructor
/// </summary>
public ConcretePrototype1(string id)
: base(id)
{
} /// <summary>
/// Returns a shallow copy
/// </summary>
/// <returns></returns>
public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}
}

  ConcretePrototype2.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.PrototypePattern.Structural
{
public class ConcretePrototype2 : Prototype
{
/// <summary>
/// Constructor
/// </summary>
public ConcretePrototype2(string id)
: base(id)
{
} /// <summary>
/// Returns a shallow copy
/// </summary>
/// <returns></returns>
public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}
}

  Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using DesignPatterns.PrototypePattern.Structural; namespace DesignPatterns.PrototypePattern
{
class Client
{
static void Main(string[] args)
{
// Create two instances and clone each
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
Console.WriteLine("Cloned: {0}", c1.Id); ConcretePrototype2 p2 = new ConcretePrototype2("II");
ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
Console.WriteLine("Cloned: {0}", c2.Id);
}
}
}

  运行输出:

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

4、原型模式实践应用

  ColorPrototype.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.PrototypePattern.Practical
{
/// <summary>
/// The 'Prototype' abstract class
/// </summary>
public abstract class ColorPrototype
{
public abstract ColorPrototype Clone();
}
}

  Color.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.PrototypePattern.Practical
{
/// <summary>
/// The 'ConcretePrototype' class
/// </summary>
public class Color : ColorPrototype
{
private int _red;
private int _green;
private int _blue; /// <summary>
/// Constructor
/// </summary>
public Color(int red, int green, int blue)
{
this._red = red;
this._green = green;
this._blue = blue;
} /// <summary>
/// Create a shallow copy
/// </summary>
public override ColorPrototype Clone()
{
Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);
return this.MemberwiseClone() as ColorPrototype;
}
}
}

  ColorManager.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.PrototypePattern.Practical
{
/// <summary>
/// Prototype manager
/// </summary>
public class ColorManager
{
private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>(); /// <summary>
/// Indexer
/// </summary>
public ColorPrototype this[string key]
{
get { return _colors[key]; }
set { _colors.Add(key, value); }
}
}
}

  Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using DesignPatterns.PrototypePattern.Practical; namespace DesignPatterns.PrototypePattern
{
class Client
{
static void Main(string[] args)
{
ColorManager colormanager = new ColorManager(); // Initialize with standard colors
colormanager["red"] = new Color(, , );
colormanager["green"] = new Color(, , );
colormanager["blue"] = new Color(, , ); // User adds personalized colors
colormanager["angry"] = new Color(, , );
colormanager["peace"] = new Color(, , );
colormanager["flame"] = new Color(, , ); // User clones selected colors
Color color1 = colormanager["red"].Clone() as Color;
Color color2 = colormanager["peace"].Clone() as Color;
Color color3 = colormanager["flame"].Clone() as Color;
}
}
}

  运行输出:

Cloning color RGB: ,  ,
Cloning color RGB: ,,
Cloning color RGB: , ,
请按任意键继续. . .

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. vim 插件之 gist.vim 的安装

    用 IntelliJ 的时觉得 create gist 很好用,查了下,发现 vim 下也有这个插件,于是马上配置上. 安装 下载 Gist.vim 解压后进入目录,拷贝文件 cp plugin/gi ...

  2. 【JBOSS】User not found SA

    启动JBOSS 发现报User not found: SA 错误, 找了老半天才找到处理方法,异常日志如下: java.sql.SQLException: User not found: SA at ...

  3. WPF上传文件到服务器

    利用WebClient 上传文件到服务器 创建一个空网站,创建一个UploadFile.aspx项, 服务器报500错误:检查文件保存路径是否存在,检查文件大小限制 protected void Pa ...

  4. 疯狂C#~伴随着我的库存管理¥

    每次的等待都是期待下一次的勃发!但激进的我非常想和大家学习一些东西,所以特地的分享了一个库存管理, 生活中容易运用的很多,但现在的学业希望能够得到各界人士的帮助!!! 首先:会有几个类来让它们协调 ( ...

  5. 浅谈PHP7新特性

    1. 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用.用法: $a = $_GET['a'] ?? 1; 它相当于: $a = isset($_GET['a']) ? $_GE ...

  6. webserver几个例子

    刚刚学习了web服务,实现了发布和调用电话号码归属地查询,下面我简单的说一下 第一个方法利用网页实现号码查询: 首先进入http://www.webxml.com.cn/网站 然后点这个 输入手机号码 ...

  7. C#中ToString格式大全

    更多资源:http://denghejun.github.io C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5&q ...

  8. C语言的傻瓜式随笔(一):嵌套循环-程序结构

    循环语句的嵌套 一个循环结构内可以含有另一个循环,称为循环嵌套,又称多重循环.常用的循环嵌套是二重循环,外层循环称为外循环,内层循环称为内循环. ---------不知道哪来的基础概念 这是本宝宝的第 ...

  9. NLP--十项沟通前的思想准备

    如何达到有效沟通?sino NLP课程给我们十项针对沟通前的思想准备,可让我们了解怎样做到效果卓越的沟通: 1.建立和谐气氛. 这是有效沟通的前提条件,只有首先建立一个和谐的气氛,双方才能彼此敞开心扉 ...

  10. 学习笔记:Hashtable和HashMap

    学了这么些天的基础知识发现自己还是个门外汗,难怪自己一直混的不怎么样.但这样的恶补不知道有没有用,是不是过段时间这些知识又忘了呢?这些知识平时的工作好像都是随拿随用的,也并不是平时一点没有关注过这些基 ...