zoukankan      html  css  js  c++  java
  • 原型模式 -- 大话设计模式

    在今天,读书有时是件“麻烦”事。它需要你付出时间,付出精力,还要付出一份心境。--仅以《大话设计模式》来祭奠那逝去的……

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

    在初始化的信息不发生变化的情况下,克隆是最好的方法。这既隐藏了对象的创建细节,又对性能有大大的提高(减少实例化等阶段的时间消耗)

    1.复印简历(浅复制Clone:如果原对象的属性是值类型,则完整复制;如果原对象的属性是引用类型,则只复制引用,两个引用使用同一对象)

      定义简历类,继承ICloneable接口

        public class Resume : ICloneable
        {
            public string Name { get; set; }
            public string Sex { get; set; }
            public string Age { get; set; }
    
            public string WorkTime { get; set; }
            public string Company { get; set; }
    
            public Resume(string name)
            {
                this.Name = name;
            }
    
            public void SetPersonalInfo(string sex, string age)
            {
                this.Sex = sex;
                this.Age = age;
            }
    
            public void SetWorkExperience(string workTime, string company)
            {
                this.WorkTime = workTime;
                this.Company = company;
            }
    
            public void Display()
            {
                Console.WriteLine("{0} {1} {2}", this.Name, this.Sex, this.Age);
                Console.WriteLine("工作经历:{0} {1}", this.WorkTime, this.Company);
            }
    
    
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }
    

       开启场景模拟

            static void Main(string[] args)
            {
                Resume lixiaoyao = new Resume("李逍遥");
                lixiaoyao.SetPersonalInfo("男", "23岁");
                lixiaoyao.SetWorkExperience("2010年", "仙剑奇侠传第一部");
                lixiaoyao.Display();
    
                Resume lixiaoyao2 = (Resume)lixiaoyao.Clone();
                lixiaoyao2.SetWorkExperience("2012年", "仙剑奇侠传第三部");
                lixiaoyao2.Display();
            }
    

     2.复印简历(深复制Clone,Resume必须提供Clone方法调用的私有构造函数,让教育经历和工作经历完成克隆,并对相关字段赋值)

      1.定义简历类,继承IConeable接口

        public class Resume : ICloneable
        {
            public string Name { get; set; }
            public string Sex { get; set; }
            public string Age { get; set; }
    
            public WorkExperience WorkExperience { get; set; }
            public EduExperience EduExperience { get; set; }
    
            public Resume(string name)
            {
                this.Name = name;
    
                WorkExperience = new WorkExperience();
                EduExperience = new EduExperience();
            }
    
            /// <summary>
            /// 提供Clone方法调用的私有构造函数
            /// </summary>
            private Resume(WorkExperience work, EduExperience edu)
            {
                this.WorkExperience = (WorkExperience)work.Clone();
                this.EduExperience = (EduExperience)edu.Clone();
            }
    
            public void SetPersonalInfo(string sex, string age)
            {
                this.Sex = sex;
                this.Age = age;
            }
    
            public void SetWorkExperience(string workTime, string company)
            {
                this.WorkExperience.WorkTime = workTime;
                this.WorkExperience.Company = company;
            }
    
            public void SetEduExperience(string studyTime, string school)
            {
                this.EduExperience.StudyTime = studyTime;
                this.EduExperience.School = school;
            }
    
            public void Display()
            {
                Console.WriteLine("{0} {1} {2}", this.Name, this.Sex, this.Age);
                Console.WriteLine("教育经历:{0} {1}", this.EduExperience.StudyTime, this.EduExperience.School);
                Console.WriteLine("工作经历:{0} {1}", this.WorkExperience.WorkTime, this.WorkExperience.Company);
            }
    
    
            public object Clone()
            {
                //调用私有构造函数,让工作经历和教育经历完成克隆,并给相关字段赋值
                Resume obj = new Resume(this.WorkExperience, this.EduExperience);
    
                obj.Name = this.Name;
                obj.Sex = this.Sex;
                obj.Age = this.Age;
    
                return obj;
            }
        }
    
        public class WorkExperience : ICloneable
        {
            public string WorkTime { get; set; }
            public string Company { get; set; }
    
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }
    
        public class EduExperience : ICloneable
        {
            public string StudyTime { get; set; }
            public string School { get; set; }
    
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }
    

       开启场景模拟

            static void Main(string[] args)
            {
                Resume lixiaoyao = new Resume("李逍遥");
                lixiaoyao.SetPersonalInfo("男", "23岁");
                lixiaoyao.SetEduExperience("2006年", "石家庄铁道大学");
                lixiaoyao.SetWorkExperience("2010年", "仙剑奇侠传第一部");
    
                Resume lixiaoyao2 = (Resume)lixiaoyao.Clone();
                lixiaoyao2.SetEduExperience("2010年", "北京喜剧学院");
                lixiaoyao2.SetWorkExperience("2012年", "仙剑奇侠传第三部");
                
                lixiaoyao.Display();
                lixiaoyao2.Display();
            }
    

      

  • 相关阅读:
    K3Cloud 解决方案版本号问题
    K3Cloud 通过元数据查询单据信息
    K3Cloud 设置分录的字段颜色
    K3Cloud 干预标准产品插件
    K3Cloud 根据单据ID 获取单据视图和数据包
    K3Cloud 后台修改账户密码策略
    K3Cloud 选择基础资料允许显示未审核数据
    K3Cloud 根据内码和基础资料元数据获取基础资料数据包
    按照应用场景划分安全测试
    常见业务场景的安全测试及安全开发
  • 原文地址:https://www.cnblogs.com/amywechat/p/4923113.html
Copyright © 2011-2022 走看看