设计模式---Prototype pattern

  • Prototype 模式
    属于创建型模式
    通过浅拷贝一个已经存在的对象,来创建一个对象,主要用于当对象的数据获取比较复杂,或获取对象成本较高时使用。

  • 代码示例

/*
 *protoType pattern
 *  Prototype pattern in which new objects 
 *  are created by copying pre-existing objects (prototypes) of the same class.                
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Pattern02
{
    class Program
    {

        static void Main(string[] args)
        {

            // Create two instances and clone each

            ConcretePrototype1 p1 = new ConcretePrototype1("A");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("cloned:{0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("B");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
            Console.WriteLine("cloned:{0}", c2.Id);
            Console.ReadLine();
        }

    }

    abstract class Prototype
    {
        private string id;//
        public string Id
        {
            get { return id; }
        }

        public Prototype(string id)
        {
            this.id = id;
        }

        public abstract Prototype Clone();
    }

    class ConcretePrototype1 : Prototype
    {
        public ConcretePrototype1(string id):base(id)
        {  }

        //返回一个shallow copy的prototype类型的对象
        public override Prototype Clone()
        {
           

            return (Prototype)this.MemberwiseClone();
        }
    }

    class ConcretePrototype2 : Prototype
    {
        public ConcretePrototype2(string id) : base(id)
        { }
        public override Prototype Clone()
        {
            return (Prototype)this.MemberwiseClone();
        }
    }
}

在这里插入图片描述

  • 例如 使用prototype 获取color 对象
/*
 *protoType pattern
 *  Prototype pattern in which new objects 
 *  are created by copying pre-existing objects (prototypes) of the same class.                
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Pattern02
{
    class Program
    {

        static void Main(string[] args)
        {
            ColorManager colorManager = new ColorManager();
            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(225, 54, 0);
            colorManager["peace"] = new Color(128, 211, 128);
            colorManager["flame"] = new Color(211, 34, 20);

            ///通过shallow copy 获取对象。
            Color color1 = colorManager["red"].Clone() as Color;
            Color color2 = colorManager["peace"].Clone() as Color;
            Color color3 = colorManager["flame"].Clone() as Color;
          
            Console.ReadLine();
        }

    }
    
    /// <summary>
    /// prototype 定义一个接口,用于shallow copy
    /// </summary>
    abstract class ColorPrototype
    {
        public abstract ColorPrototype Clone();
    }
    /// <summary>
    /// concretePrototype 实现接口,用于shallow copy
    /// </summary>
    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;
        }
        //返回一个shallow copy 获取到的ColorPrototype 对象
        public override ColorPrototype Clone()
        {
            Console.WriteLine(
     "Cloning color RGB: {0,3},{1,3},{2,3}",
     _red, _green, _blue);
            return this.MemberwiseClone() as ColorPrototype;
        }
    }
    /// <summary>
    /// client 通过protoType的shallow copy 得到新的对象。
    /// </summary>
    class ColorManager
    {
        private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>();

        //indexer 索引器
        public ColorPrototype this[string key]
        {
            get { return _colors[key]; }
            set { _colors.Add(key, value); }
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值