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();
            }
    

      

  • 相关阅读:
    HDU 3081 Marriage Match II
    HDU 4292 Food
    HDU 4322 Candy
    HDU 4183 Pahom on Water
    POJ 1966 Cable TV Network
    HDU 3605 Escape
    HDU 3338 Kakuro Extension
    HDU 3572 Task Schedule
    HDU 3998 Sequence
    Burning Midnight Oil
  • 原文地址:https://www.cnblogs.com/amywechat/p/4923113.html
Copyright © 2011-2022 走看看