Net设计模式实例之原型模式( Prototype Pattern)

简介: 一、原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。 Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype。

一、原型模式简介(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、具体实现类ConcretePrototypeAConcretePrototypeB

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

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

img_2c313bac282354945ea179a807d7e70d.jpg

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

相关文章
|
C# 设计模式 .NET
使用C# (.NET Core) 实现状态设计模式 (State Pattern)
本文的概念性内容来自深入浅出设计模式一书 项目需求 这是一个糖果机的需求图.  它有四种状态, 分别是图中的四个圆圈: No Quarter: 无硬币 Has Quater 有硬币 Gumball Sold 糖果卖出 Out of Gumball 没有糖果了 这个图很像一个状态图.
1781 0
|
C#
使用C# (.NET Core) 实现组合设计模式 (Composite Pattern)
本文的概念性内容来自深入浅出设计模式一书. 本文需结合上一篇文章(使用C# (.NET Core) 实现迭代器设计模式)一起看. 上一篇文章我们研究了多个菜单一起使用的问题. 需求变更 就当我们感觉我们的设计已经足够好的时候, 新的需求来了, 我们不仅要支持多种菜单, 还要支持菜单下可以拥有子菜单.
1403 0
|
Java C# 设计模式
使用C# (.NET Core) 实现迭代器设计模式 (Iterator Pattern)
本文的概念来自深入浅出设计模式一书 项目需求 有两个饭店合并了, 它们各自有自己的菜单. 饭店合并之后要保留这两份菜单. 这两个菜单是这样的: 菜单项MenuItem的代码是这样的: 最初我们是这样设计的, 这是第一份菜单: 这是第2份菜单: 同时有两个菜单存在的问题 问题就是多个菜单把事情变复杂了.
1003 0
|
算法 C# Java
使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, 另外两步虽然具体内容不一样, 但是都做做的同一类工作.
1309 0
|
Java C#
使用C# (.NET Core) 实现适配器模式 (Adapter Pattern) 和外观模式 (Facade Pattern)
本文的概念内容来自深入浅出设计模式一书 现实世界中的适配器(模式) 我带着一个国标插头的笔记本电脑, 来到欧洲, 想插入到欧洲标准的墙壁插座里面, 就需要用中间这个电源适配器. 面向对象的适配器 你有个老系统, 现在来了个新供应商的类, 但是它们的接口不同, 如何使用这个新供应商的类呢? 首先, 我们不想修改现有代码, 你也不能修改供应商的代码.
1722 0
|
C#
使用C# (.NET Core) 实现命令设计模式 (Command Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有这样一个可编程的新型遥控器, 它有7个可编程插槽, 每个插槽可连接不同的家用电器设备. 每个插槽对应两个按钮: 开, 关(ON, OFF).
820 0
|
Java C#
使用C# (.NET Core) 实现单体设计模式 (Singleton Pattern)
本文的概念内容来自深入浅出设计模式一书 由于我在给公司做内培, 所以最近天天写设计模式的文章.... 单体模式 Singleton 单体模式的目标就是只创建一个实例. 实际中有很多种对象我们可能只需要它们的一个实例, 例如: 线程池,缓存, 弹出的对话框, 用于保存设置的类, 用于logging的类, 硬件设备驱动对象等等.
1178 0
|
C#
使用C# (.NET Core) 实现抽象工厂设计模式 (Abstract Pattern)
本文的概念性内容来自深入浅出设计模式一书. 上一篇文章讲了简单工厂和工厂方法设计模式 http://www.cnblogs.com/cgzl/p/8760250.html, 使用的是披萨店的例子. 文将继续使用这个例子, 这里要用到抽象工厂.
1334 0
|
C# 设计模式 .NET
使用C# (.NET Core) 实现简单工厂(Simple Factory) 和工厂方法设计模式 (Factory Method Pattern)
本文源自深入浅出设计模式. 只不过我是使用C#/.NET Core实现的例子.   前言 当你看见new这个关键字的时候, 就应该想到它是具体的实现. 这就是一个具体的类, 为了更灵活, 我们应该使用的是接口(interface).
1401 0
|
安全 C# 数据安全/隐私保护
使用C# (.NET Core) 实现装饰模式 (Decorator Pattern) 并介绍 .NET/Core的Stream
该文章综合了几本书的内容. 某咖啡店项目的解决方案 某咖啡店供应咖啡, 客户买咖啡的时候可以添加若干调味料, 最后要求算出总价钱. Beverage是所有咖啡饮料的抽象类, 里面的cost方法是抽象的.
1346 0