原型模式

67 阅读2分钟

原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

一般在初始化的信息不发生变化的情况下,克隆是最好的办法。这既隐藏了对象创建的细节,又对性能是大大的提高。
string类型,对字段执行逐位复制,字段是引用类型,则复制引用但不复制引用的对象。因此,原始对象及其复本引用同一对象。
浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
深复制,深复制把引用对象的变量指向复制过的对象,而不是原有的被引用的对象。

原型模式的简历代码:

class Program
    {
        static void Main(string[] args)
        {
            Resume a = new Resume("大鸟");
            a.SetPersonalInfo("男", "29");
            a.SetWorkExperience("1998-2000", "xx公司");

            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("1998-2006", "YY企业");
            a.Display();
            b.Display();
        }
    }
    class Resume
    {
        private string name;
        private string sex;
        private string age;
        private string timeArea;
        private string company;
        public Resume(string name)
        {
            this.name = name;
        }
        //设置个人信息
        public void SetPersonalInfo(string sex, string age)
        {
            this.sex = sex;
            this.age = age; 
        }
        //设置工作经历
        public void SetWorkExperience(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();
        }
    }

深复制的简历代码:

class WorkExperience : ICloneable
    {
        private string workDate;//工作日期
        public string WorkDate
        {
            get { return workDate; }
            set { workDate = value; }
        }
        private string company;
        public string Company
        {
            get { return company; }
            set { company = value; }
        }
        public object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }
    class Resume : ICloneable
    {
        private string name;
        private string sex;
        private string age;
        private WorkExperience work;
        public Resume(string name)
        {
            this.name = name;
            work = new WorkExperience();
        }
        //提供Clone方法调用的私有构造函数 以便克隆“工作经历”的数据
        private Resume(WorkExperience work)
        {
            this.work = (WorkExperience)work.Clone();
        }
        public void SetPersonalInfo(string sex, string age)
        {
            this.sex = sex;
            this.age = age;
        }
        public void SetWorkExperience(string workDate, string company)
        {
            work.WorkDate = workDate;
            work.Company = company;
        }
        public void Display()
        {
            Console.WriteLine("{0}{1}{2}", name, sex, age);
            Console.WriteLine("工作经历:{0},{1}", work.WorkDate, work.Company);
        }
        public object Clone()
        {
            Resume obj = new Resume(this.work);
            obj.name = this.name;
            obj.sex = this.sex;
            obj.age = this.age;
            return obj;
        }
    }