Design Pattern - Prototype(C#)

来源:互联网 发布:shell连接oracle数据库 编辑:程序博客网 时间:2024/05/21 15:44

Definition

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

Participants

    The classes and/or objects participating in this pattern are:

  • Prototype  (ColorPrototype)
    • Declares an interface for cloning itself
  • ConcretePrototype  (Color)
    • Implements an operation for cloning itself
  • Client  (ColorManager)
    • Creates a new object by asking a prototype to clone itself

Sample Code in C#


This structural code demonstrates the Prototype pattern in which new objects are created by copying pre-existing objects (prototypes) of the same class.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Prototype Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Structural Prototype Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Create two instances and clone each            var p1 = new ConcretePrototype1("I");            var c1 = (ConcretePrototype1)p1.Clone();            Console.WriteLine("Cloned: {0}", c1.Id);            var p2 = new ConcretePrototype2("II");            var c2 = (ConcretePrototype2)p2.Clone();            Console.WriteLine("Cloned: {0}", c2.Id);        }        #endregion    }    /// <summary>    /// The 'Prototype' abstract class    /// </summary>    internal abstract class Prototype    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Prototype"/> class.        /// </summary>        /// <param name="id">        /// The id.        /// </param>        protected Prototype(string id)        {            this.Id = id;        }        #endregion        // Gets id        #region Public Properties        /// <summary>        /// Gets the id.        /// </summary>        public string Id { get; private set; }        #endregion        #region Public Methods and Operators        /// <summary>        /// The clone.        /// </summary>        /// <returns>        /// The <see cref="Prototype"/>.        /// </returns>        public abstract Prototype Clone();        #endregion    }    /// <summary>    /// A 'ConcretePrototype' class    /// </summary>    internal class ConcretePrototype1 : Prototype    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="ConcretePrototype1"/> class.        /// </summary>        /// <param name="id">        /// The id.        /// </param>        public ConcretePrototype1(string id)            : base(id)        {        }        #endregion        // Returns a shallow copy        #region Public Methods and Operators        /// <summary>        /// The clone.        /// </summary>        /// <returns>        /// The <see cref="Prototype"/>.        /// </returns>        public override Prototype Clone()        {            return (Prototype)this.MemberwiseClone();        }        #endregion    }    /// <summary>    /// A 'ConcretePrototype' class    /// </summary>    internal class ConcretePrototype2 : Prototype    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="ConcretePrototype2"/> class.        /// </summary>        /// <param name="id">        /// The id.        /// </param>        public ConcretePrototype2(string id)            : base(id)        {        }        #endregion        // Returns a shallow copy        #region Public Methods and Operators        /// <summary>        /// The clone.        /// </summary>        /// <returns>        /// The <see cref="Prototype"/>.        /// </returns>        public override Prototype Clone()        {            return (Prototype)this.MemberwiseClone();        }        #endregion    }}// Output:/*Cloned: ICloned: II*/


This real-world code demonstrates the Prototype pattern in which new Color objects are created by copying pre-existing, user-defined Colors of the same type.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Prototype Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections.Generic;    /// <summary>    /// Startup class for Real-World Prototype Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            var colormanager = new ColorManager();            // Initialize with standard colors            colormanager["red"] = new Color(255, 0, 0);            colormanager["green"] = new Color(0, 255, 0);            colormanager["blue"] = new Color(0, 0, 255);            // User adds personalized colors            colormanager["angry"] = new Color(255, 54, 0);            colormanager["peace"] = new Color(128, 211, 128);            colormanager["flame"] = new Color(211, 34, 20);            // User clones selected colors            var color1 = colormanager["red"].Clone() as Color;            var color2 = colormanager["peace"].Clone() as Color;            var color3 = colormanager["flame"].Clone() as Color;        }        #endregion    }    /// <summary>    /// The 'Prototype' abstract class    /// </summary>    internal abstract class ColorPrototype    {        #region Public Methods and Operators        /// <summary>        /// The clone.        /// </summary>        /// <returns>        /// The <see cref="ColorPrototype"/>.        /// </returns>        public abstract ColorPrototype Clone();        #endregion    }    /// <summary>    /// The 'ConcretePrototype' class    /// </summary>    internal class Color : ColorPrototype    {        #region Fields        /// <summary>        /// The blue.        /// </summary>        private int blue;        /// <summary>        /// The green.        /// </summary>        private int green;        /// <summary>        /// The red.        /// </summary>        private int red;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Color"/> class.        /// </summary>        /// <param name="red">        /// The red.        /// </param>        /// <param name="green">        /// The green.        /// </param>        /// <param name="blue">        /// The blue.        /// </param>        public Color(int red, int green, int blue)        {            this.red = red;            this.green = green;            this.blue = blue;        }        #endregion        // Create a shallow copy        #region Public Methods and Operators        /// <summary>        /// The clone.        /// </summary>        /// <returns>        /// The <see cref="ColorPrototype"/>.        /// </returns>        public override ColorPrototype Clone()        {            Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", this.red, this.green, this.blue);            return this.MemberwiseClone() as ColorPrototype;        }        #endregion    }    /// <summary>    /// Prototype manager    /// </summary>    internal class ColorManager    {        #region Fields        /// <summary>        /// The colors.        /// </summary>        private Dictionary<string, ColorPrototype> colors = new Dictionary<string, ColorPrototype>();        #endregion        // Indexer        #region Public Indexers        /// <summary>        /// The this.        /// </summary>        /// <param name="key">        /// The key.        /// </param>        /// <returns>        /// The <see cref="ColorPrototype"/>.        /// </returns>        public ColorPrototype this[string key]        {            get            {                return this.colors[key];            }            set            {                this.colors.Add(key, value);            }        }        #endregion    }}// Output:/*Cloning color RGB: 255, 0, 0Cloning color RGB: 128,211,128Cloning color RGB: 211, 34, 20*/

原创粉丝点击