第 9 章 原型模式

原型模式:用原型实例制定建立对象的种类,而且经过拷贝这些原型建立新的对象。性能

 

原型模式其实就是从一个对象再建立另一个可定制的对象,并且不须要知道任何建立的细节。
 
.Net在System命名空间中提供了ICloneable接口,其中就是惟一的一个方法Clone(),这样你就只须要实现这个接口就完成原型模式了。
public class Resume : ICloneable
    {
        private string sex;
        private string age;
        private string timeArea;
        private string company;

        //设置我的信息
        public void SetPersonalInfo(string sex, string age)
        {
            this.sex = sex;
            this.age = age;
        }

        //设置工做经历
        public void SetWorkExprience(string timeArea, string company)
        {
            this.timeArea = timeArea;
            this.company = company;
        }

        //显示
        public void Display()
        {
            Console.WriteLine("我的信息:{0} {1} {2}", name, sex, age);
            Console.WriteLine("工做经验:{0} {1}", timeArea, company);
        }

        //克隆
        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }
通常在初始化的信息不发生变化的状况下,克隆是最好的办法,这既隐瞒了对象建立的细节,又对性能是大大的提升。
不用从新初始化对象,而是动态的得到对象运行时的状态。
 
浅复制和深复制:
MemberwiseClone()方法是这样,若是字段是值类型的,则对该字段执行逐位复制。若是字段是引用类型,则复制引用但不复制引用的对象;所以,原始对象及其副本引用同一个对象。
 
代码结构变化:新建工做经验类
public class Resume : ICloneable
    {
        private string name;
        private string sex;
        private string age;
        private WorkExperience workExperience;
        public Resume(string name)
        {
            this.name = name;
            this.workExperience = new WorkExperience();
        }

        //设置我的信息
        public void SetPersonalInfo(string sex, string age)
        {
            this.sex = sex;
            this.age = age;
        }

        //设置工做经历
        public void SetWorkExprience(string timeArea, string company)
        {
            workExperience.WorkDate = timeArea;
            workExperience.Company = company;
        }

        //显示
        public void Display()
        {
            Console.WriteLine("我的信息:{0} {1} {2}", name, sex, age);
            Console.WriteLine("工做经验:{0} {1}", workExperience.WorkDate, workExperience.Company);
        }

        //克隆
        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }

    /// <summary>
    /// 工做经验类
    /// </summary>
    class WorkExperience
    {
        public string WorkDate { get; set; }
        public string Company { get; set; }
    }

客户端代码:this

Resume r = new Resume("张三");
r.SetPersonalInfo("", "25");
r.SetWorkExprience("2010", "嘻嘻");
var rr = (Resume)r.Clone();
rr.SetWorkExprience("2014", "xx嘻");


 r.Display();
 rr.Display();
 Console.ReadLine();

由于 MemberwiseClone只是浅表复制,因此结果为:spa

浅复制,被复制的对象的全部变量都含有与原来的对象相同的值,而全部的对其余对象的引用都仍然指向原来的对象。可是咱们可能更须要这样的一个需求,把要复制的对象所引用的对象都复制一遍。
深复制:把引用对象的变量指向复制过来的新对象,而不是原来的被引用的对象。
 
改为深度复制:
 public class Resume : ICloneable
    {
        private string name;
        private string sex;
        private string age;
        private WorkExperience workExperience;
        public Resume(string name)
        {
            this.name = name;
            this.workExperience = new WorkExperience();
        }

        public Resume(WorkExperience workExperience)
        {
            this.workExperience = (WorkExperience)workExperience.Clone();
        }

        //设置我的信息
        public void SetPersonalInfo(string sex, string age)
        {
            this.sex = sex;
            this.age = age;
        }

        //设置工做经历
        public void SetWorkExprience(string timeArea, string company)
        {
            workExperience.WorkDate = timeArea;
            workExperience.Company = company;
        }

        //显示
        public void Display()
        {
            Console.WriteLine("我的信息:{0} {1} {2}", name, sex, age);
            Console.WriteLine("工做经验:{0} {1}", workExperience.WorkDate, workExperience.Company);
        }

        //克隆
        public Object Clone()
        {
            Resume r = new Resume(this.workExperience);
            r.name = this.name;
            r.sex = this.sex;
            r.age = this.age;
            return r;
        }
    }

    /// <summary>
    /// 工做经验类
    /// </summary>
    public class WorkExperience
    {
        public string WorkDate { get; set; }
        public string Company { get; set; }
        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }

结果:code

相关文章
相关标签/搜索