码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之原型模式

时间:2015-04-07 13:36:48      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

原型模式

创建型模式

用来处理对象的构造,通过克隆的方法复制自身,动态的修改对象在运行时的状态,从而降低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 只通过复制的方法动态的修改 对应运行的状态

 

设计模式之原型模式

标签:

原文地址:http://www.cnblogs.com/Sky-cloudless/p/4397829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!