⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 来自DoFactory的设计模式, 由于是本人根据已有代码创建,没有NETOptimized项
💻 CS
字号:
// Prototype pattern -- Real World example  

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Prototype.RealWorld
{

    // MainApp test application 

    class MainApp
    {
        static void Main()
        {
            ColorManager 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);

            Color color;

            // User uses selected colors 
            string name = "red";
            color = colormanager[name].Clone() as Color;

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

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

            // Wait for user 
            Console.Read();
        }
    }

    // "Prototype" 

    abstract class ColorPrototype
    {
        public abstract ColorPrototype Clone();
    }

    // "ConcretePrototype" 

    class Color : ColorPrototype
    {
        private int red;
        private int green;
        private int blue;

        // Constructor 
        public Color(int red, int green, int blue)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
        }

        // Create a shallow copy 
        public override ColorPrototype Clone()
        {
            Console.WriteLine(
              "Cloning color RGB: {0,3},{1,3},{2,3}",
              red, green, blue);

            return this.MemberwiseClone() as ColorPrototype;
        }
    }

    // Prototype manager 

    class ColorManager
    {
        Hashtable colors = new Hashtable();

        // Indexer 
        public ColorPrototype this[string name]
        {
            get
            {
                return colors[name] as ColorPrototype;
            }
            set
            {
                colors.Add(name, value);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -