• 第 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; }
        }

    客户端代码:

    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只是浅表复制,所以结果为:

    浅复制,被复制的对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。但是我们可能更需要这样的一个需求,把要复制的对象所引用的对象都复制一遍。
    深复制:把引用对象的变量指向复制过来的新对象,而不是原来的被引用的对象。
     
    改成深度复制:
     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();
            }
        }

    结果:

  • 相关阅读:
    XPath 入门
    用jQuery为页面添加活力
    将xml中的数据导入到数据库
    web.config 电邮配置
    一、创建Cuisl.dll工程
    使用ASP.NET服务器控件
    VSTO install error 0x80131604
    javaScript 5
    CSS 基础
    创建第一个ASP.NET网站
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/4016411.html
Copyright © 2020-2023  润新知