一、原型模式简介(Brief Introduction)

原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype。

浅复制与深复制区别:

浅复制,被复制的所有变量都还有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。

深复制,把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。

Net命名空间System提供了一个IConeable接口,此接口只有一个方法Clone(),只需要实现这个接口就可以实现原型模式(Prototype Pattern)了。

二、解决的问题(What To Solve)

当一个对象生成不是通过New而是通过复制旧对象的时候,可以考虑使用原型模式。

三、原型模式分析(Analysis)

1、原型模式结构

 

Prototype:原型类 Clone()方法:克隆自身的接口。

ConcretePrototypeAConcretePrototypeA:原型类的具体实现。克隆一个自身的操作。

2、代码

1、原型抽象类Prototype

/// <summary>

/// 抽象原型模式类

/// </summary>

public abstract class Prototype

{

private string _id;

public Prototype(string id)

{

this.Id = id;

}

public string Id

{

get { return _id; }

set { _id = value; }

}

public abstract Prototype Clone();

}

2、具体实现类ConcretePrototypeA和ConcretePrototypeB

public class ConcretePrototypeA : Prototype

{

public ConcretePrototypeA(string id)

: base(id)

{ }

/// <summary>

/// Returns a shallow copy 浅拷贝

/// </summary>

/// <returns>a shallow copy</returns>

public override Prototype Clone()

{

return (Prototype)this.MemberwiseClone();

}

}

public class ConcretePrototypeB:Prototype

{

public ConcretePrototypeB(string id)

: base(id)

{ }

/// <summary>

/// a shallow copy

/// </summary>

/// <returns>浅拷贝</returns>

public override Prototype Clone()

{

return (Prototype)this.MemberwiseClone();

}

}

2、客户端代码

static void Main(string[] args)

{

Prototype pA = new ConcretePrototypeA("A");

Prototype cA = pA.Clone() as ConcretePrototypeA ;

Console.WriteLine("Cloned:"+cA.Id);

ConcretePrototypeB pB = new ConcretePrototypeB("B");

ConcretePrototypeB cB = (ConcretePrototypeB)pB.Clone();

Console.WriteLine("Cloned:"+cB.Id);

Console.ReadKey();

}

3、实例运行结果

四.原型模式实例分析(Example)

1、场景

颜色索引器存储多种颜色值,从颜色索引器中克隆客户需要几种颜色。结构如下图所示

ColorManager:颜色索引器

ColorPrototype:原型模式抽象类

Color:原型模式抽象类的具体实现,Clone方法的实现,克隆自身的操作

2、代码

1、原型模式抽象类ColorPrototype及其具体实现类Color

/// <summary>

/// 原型模式抽象类

/// </summary>

public abstract class ColorPrototype

{

public abstract ColorPrototype Clone();

}

/// <summary>

/// 具体实现类

/// </summary>

public class Color : ColorPrototype

{

private int _red;

private int _green;

private int _blue;

public Color(int red, int green, int blue)

{

this._red = red;

this._green = green;

this._blue = blue;

}

/// <summary>

/// 实现浅复制

/// </summary>

/// <returns></returns>

public override ColorPrototype Clone()

{

Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);

return this.MemberwiseClone() as ColorPrototype;

}

}

3、客户端代码

static void Main(string[] args)

{

ColorManager colormanager = new ColorManager();

//初始化标准的red green blue颜色。

colormanager["red"] = new Color(255, 0, 0);

colormanager["green"] = new Color(0, 255, 0);

colormanager["blue"] = new Color(0, 0, 255);

// 添加个性的颜色

colormanager["angry"] = new Color(255, 54, 0);

colormanager["peace"] = new Color(128, 211, 128);

colormanager["flame"] = new Color(211, 34, 20);

// 克隆颜色

Color color1 = colormanager["red"].Clone() as Color;

Color color2 = colormanager["peace"].Clone() as Color;

Color color3 = colormanager["flame"].Clone() as Color;

Console.ReadKey();

}

3、实例运行结果

五、总结(Summary)

本文对原型模式(Prototype Pattern)的概念、设计结构图、代码、使用场景、深复制与浅复制的区别,以及如何Net平台下实现克隆进行了描述。以一个实例进行了说明。原型模式是比较常用和简单的设计模式。

转载自:http://www.cnblogs.com/ywqu/archive/2010/01/12/1644550.html

Net设计模式实例之原型模式( Prototype Pattern)的更多相关文章

  1. 设计模式系列之原型模式(Prototype Pattern)——对象的克隆

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

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

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

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

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

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

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

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

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

  6. C#设计模式——原型模式(Prototype Pattern)

    一.概述 在软件开发中,经常会碰上某些对象,其创建的过程比较复杂,而且随着需求的变化,其创建过程也会发生剧烈的变化,但他们的接口却能比较稳定.对这类对象的创建,我们应该遵循依赖倒置原则,即抽象不应该依 ...

  7. 2.6 《硬啃设计模式》第8章 复制不是很难 - 原型模式(Prototype Pattern)

    案例: 某即时战略游戏,你训练出来各种很强的战士. 为了增加游戏的可玩性,增加了一种复制魔法.实施该魔法,可以复制任意的战士. 你会怎样考虑这个设计? 在继续阅读之前,请先认真思考并写出你的设计,这样 ...

  8. 设计模式——原型模式(Prototype Pattern)

    原型模式:用原型实例制定创建对象的种类,并且通过拷贝这些原型创建新的对象. UML 图: 原型类: package com.cnblog.clarck; /** * 原型类 * * @author c ...

  9. 设计模式学习心得<原型模式 Prototype >

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式是实现了一个原型接口,该接口用于创建当 ...

随机推荐

  1. jquery 选择元素

  2. vue组件编译原理

    <body> <my-com1 type="type" v-pre="pre">com1</my-com1> <my- ...

  3. C#_技巧:真伪随机数

    使用 Random 产生随机数.(这是一种伪随机数,需要seed,同一个seed后,采用某种算法产生的数字序列都是一样的) 两种写法 错误 for(int i=0;i<100;i++) {    ...

  4. AngularJS SPA Template For Visual Studio

    单页面应用程序(SPA)[使用JavaScript.CSS和HTML强大的功能,可以构建一个单页面应用程序(SPAs)],它提供了丰富的用户体验页面.导航技术和AJAX提供必要的功能,而不用重新加载页 ...

  5. Lesson 5 No wrong numbers

    Text Mr.James Scott has a garage in Silbury and now he has just bought another garage in Pinhurst. P ...

  6. 剑指Offer面试题:2.二维数组中的查找

    一.题目:二维数组中的查找 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...

  7. char varchar nchar nvarcharar到底有多大区别

    首先说明下,ASP.NET MVC系列还在龟速翻译中. 工作好多年,基础知识甚是薄弱,决定以后在coding(cv操作)的时候尽量多google下,然后总结下来,目的有三:     1. 加深自己的理 ...

  8. WCF Security基本概念(转载)

    WCF Security 主要包括 "Transfer Security"."Access Control"."Auditing" 几个部分 ...

  9. 《Entity Framework 6 Recipes》中文翻译系列 (22) -----第五章 加载实体和导航属性之延迟加载

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第五章 加载实体和导航属性 实体框架提供了非常棒的建模环境,它允许开发人员可视化地使 ...

  10. 带你走近AngularJS - 基本功能介绍

    带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...