Design_Pattern-Prototype

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Design_Patterns
{

    //在软件系统中,经常面临着“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是它们却拥有比较稳定一致的接口。
    //大量单据生成是可以使用 拷贝 (关于批量生成对象时可以使用原型模式)
    //Prototype模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求 这些“易变类”拥有“稳定的接口”。
    //Prototype模式对于“如何创建易变类的实体对象”采用“原型克隆”的方法来做,它使得我们可以非 常灵活地动态创建“拥有某些稳定接口”的新对象——所需工作仅仅是注册一个新类的对象(即 原型),然后在任何需要的地方不断地Clone。
    //Prototype模式中的Clone方法可以利用.NET中的 Object类的MemberwiseClone()方法或者序列化来实现深拷贝。

    //对象 剧烈变化 接口不变
    abstract class ColorPrototype
    {
        public abstract ColorPrototype Clone();
    }

    class Color : ColorPrototype
    {
        int Red;
        int Green;
        int Blue;

        public Color(int red, int green, int blue)
        {
            this.Red = red;
            this.Green = green;
            this.Blue = blue;
        }

        public override ColorPrototype Clone()
        {
            Console.WriteLine("Cloning color RGB:{0,3},{1,3},{2,3}", Red, Green, Blue);
            return this.MemberwiseClone() as ColorPrototype;//浅表副本
        }
    }

    class ColorManager
    {
        Hashtable colors = new Hashtable();

        public ColorPrototype this[string name]
        {
            get
            {
                return colors[name] as ColorPrototype;
            }
            set
            {
                colors.Add(name, value);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值