zoukankan      html  css  js  c++  java
  • 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

    浅复制与深复制区别:

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

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

    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平台下实现克隆进行了描述。以一个实例进行了说明。原型模式是比较常用和简单的设计模式。

  • 相关阅读:
    1058 A+B in Hogwarts (20)
    1036. Boys vs Girls (25)
    1035 Password (20)
    1027 Colors in Mars (20)
    1009. Product of Polynomials (25)
    1006. Sign In and Sign Out
    1005 Spell It Right (20)
    1046 Shortest Distance (20)
    ViewPager页面滑动,滑动到最后一页,再往后滑动则执行一个事件
    IIS7.0上传文件限制的解决方法
  • 原文地址:https://www.cnblogs.com/ywqu/p/1644550.html
Copyright © 2011-2022 走看看