设计模式之原型模式

原型模式

创建型模式

用来处理对象的构造,通过克隆的方法复制自身,动态的修改对象在运行时的状态,从而降低new带来的性能消耗

从复制简历开始说起

1.0代码

public class Resume
    {
        private string name;
        private string sex;
        private int age;

        private string timeArea;
        private string company;

        public Resume(string name)
        {
            this.name = name;
        }

        public void SetPersonalInfo(string sex, int age)
        {
            this.sex = sex;
            this.age = age;
        }

        public void SetWorkExperience(string timerArea, string company)
        {
            this.company = company;
            this.timeArea = timerArea;
        }

        public void Display()
        {
            Console.WriteLine("{0} {1} {2}", name, sex, age);
            Console.WriteLine("工作经历:{0} {1}", timeArea, company);
        }
    }
客户端的调用如下
            Resume a = new Resume("大鸟");
            a.SetPersonalInfo("", 29);
            a.SetWorkExperience("1998-2000", "XX公司");

            Resume b = new Resume("大鸟");
            b.SetPersonalInfo("", 29);
            b.SetWorkExperience("1998-2000", "XX公司");

            Resume c = new Resume("大鸟");
            c.SetPersonalInfo("", 29);
            c.SetWorkExperience("1998-2000", "XX公司");
            a.Display();
            b.Display();
            c.Display();
稍做修改
            Resume a = new Resume("大鸟");
            a.SetPersonalInfo("", 29);
            a.SetWorkExperience("1998-2000", "XX公司");

            Resume b = a;

            Resume c = a;
            a.Display();
            b.Display();
            c.Display();

2.0 原型模式出现

结构类图如下

代码如下

  public abstract class Prototype
    {
        private string id;
        public Prototype(string id)
        {
            this.id = id;
        }
        public string ID { get { return id; } }
        public abstract Prototype Clone();
    }
    public class ClassPrototype1 : Prototype
    {
        public ClassPrototype1(string id)
            : base(id)
        {

        }
        public override Prototype Clone()
        {
            return (Prototype)this.MemberwiseClone();//对自身进行复制
        }
    }

客户端

ClassPrototype1 c1 = new ClassPrototype1("1000");
ClassPrototype1 c2 = (ClassPrototype1)c1.Clone();

Console.WriteLine("{0}",c2.ID);

3.0 使用.NET中已经有的接口ICloneable

public object Clone()
{
    return (object)this.MemberwiseClone();
}

客户端如下

            Resume a = new Resume("大鸟");
            a.SetPersonalInfo("", 29);
            a.SetWorkExperience("1998-2000", "XX公司");

            Resume b = (Resume)a.Clone();
            a.SetPersonalInfo("", 28);
            a.SetWorkExperience("1998-2006", "YY公司");

            Resume c = (Resume)a.Clone();
            a.SetPersonalInfo("", 20);
            a.SetWorkExperience("1998-2006", "ZZ公司");

            a.Display();
            b.Display();
            c.Display();

好处没有了对象的new 只通过复制的方法动态的修改 对应运行的状态

 

转载于:https://www.cnblogs.com/Sky-cloudless/p/4397829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值