zoukankan      html  css  js  c++  java
  • 设计模式のPrototypePattern(原型模式)----创建模式

    一、产生的背景

      这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。

    用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

    二、通常做法

       1、实现克隆操作,在 JAVA 继承 Cloneable,重写 clone(),在 .NET 中可以使用 Object 类的 MemberwiseClone() 方法来实现对象的浅拷贝或通过序列化的方式来实现深拷贝。

       2、原型模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求这些"易变类"拥有稳定的接口。

    三、实例

    1、建立颜色的原型

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

    2、建立颜色

        public class Color : ColorPrototype
        {
            private int _red;
            private int _green;
            private int _blue;
    
            /// <summary>
            /// Constructor
            /// </summary>
            public Color(int red, int green, int blue)
            {
                this._red = red;
                this._green = green;
                this._blue = blue;
            }
    
            /// <summary>
            /// Create a shallow copy
            /// </summary>
            public override ColorPrototype Clone()
            {
                Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);
                return this.MemberwiseClone() as ColorPrototype;
            }
        }

    3、建立管理器

    namespace ProtoPattern
    {
        public class ColorManager
        {
            private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>();
    
            /// <summary>
            /// Indexer
            /// </summary>
            public ColorPrototype this[string key]                  //索引器
            {
                get { return _colors[key]; }
                set { _colors.Add(key, value); }
            }
        }
    }

    4、客户端调用

    namespace ProtoPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                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);
    
                // User clones selected colors
                Color color1 = colormanager["red"].Clone() as Color;
                Color color2 = colormanager["peace"].Clone() as Color;
                Color color3 = colormanager["flame"].Clone() as Color;
    
                Console.ReadLine();
            }
        }
    }

    四、设计模式分析

    优点:   1、性能提高。

        2、逃避构造函数的约束。

    缺点:   1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。

             2、必须实现 Cloneable 接口。

             3、逃避构造函数的约束。

    点击下载源码

  • 相关阅读:
    LeetCode120 Triangle
    LeetCode119 Pascal's Triangle II
    LeetCode118 Pascal's Triangle
    LeetCode115 Distinct Subsequences
    LeetCode114 Flatten Binary Tree to Linked List
    LeetCode113 Path Sum II
    LeetCode112 Path Sum
    LeetCode111 Minimum Depth of Binary Tree
    Windows下搭建PHP开发环境-WEB服务器
    如何发布可用于azure的镜像文件
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/8135488.html
Copyright © 2011-2022 走看看