• 设计模式--原型模式


    class Resume
    {
        private string name;
        private sring  sex;
        private string age;
        private string timeArea;
        private string company;
    
        public Resume(string name)
        {
            this.name = name;
        }
    
        //设置个人信息
        public void SetPersonalInof(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);
        }
    }
    

    调用代码

    static void Main(string[] args)
    {
        Resume a = new Resume("大鸟");
        a.SetPersonalInof("男","29");
        a.SetWorkExperience("1998-2000","xx公司");
    
        Resume b = new Resume("大鸟");
        b.SetPersonalInof("男","29");
        b.SetWorkExperience("1998-2000","xx公司");
    
        Resume c = new Resume("大鸟");
        c.SetPersonalInof("男","29");
        c.SetWorkExperience("1998-2000","xx公司");
    
        a.Display();
        b.Display();
        c.Display();
    
        Console.Read();
    }
    

    原型模式就是从一个对象再创建另一个可定制对象,而且不需要指导任何创建的细节。

    abstract class Prototype
    {
        private string id;
    
        public Prototype(string id)
        {
            this.id = id;
        }
    
        public string Id
        {
            get{ return id; }
        }
    
        public abstract Prototype Clone();
    }
    
    
    class ConcretePrototype1:Prototype
    {
        public ConcretePrototype1(string id):base(id)
        {
    
        }
    
        public override Prototype Clone()
        {
            return (Prototype)this.MemberwiseClone();
        }
    }
    
    static void Main(string[] args)
    {
        ConcretePrototype1 p1 = new ConcretePrototype1("I");
        ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
        Console.WriteLine("Cloned:{0}",c1.Id);
    
        Console.Read();
    }
    

    例子原型

    class Resume:ICloneable
    {
        private string name;
        private sring  sex;
        private string age;
        private string timeArea;
        private string company;
    
        public Resume(string name)
        {
            this.name = name;
        }
    
        //设置个人信息
        public void SetPersonalInof(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();
        }
    }
    

    客户端调用代码

    static void Main(string[] args)
    {
        Resume a = new Resume("大鸟");
        a.SetPersonalInof("男","29");
        a.SetWorkExperience("1998-2000","xx公司");
    
        Resume b = (Resume) a.Clone;
        b.SetWorkExperience("1998-2006","yy公司");
    
        Resume c = (Resume) a.Clone;
        c.SetPersonalInof("男","29");
    
        
        a.Display();
        b.Display();
        c.Display();
    
        Console.Read();
    }
    
  • 相关阅读:
    LRU
    c++ 在临时变量上使用const引用
    剑指 Offer 13. 机器人的运动范围
    C++之对象包含与成员函数不兼容的类型限定符
    C#獲取指定格式日期
    在ORACLE產生001,002的流水號
    ASP.NET中DataList数字分页代码
    生成條碼標的Class
    sql 将横的记录显示为竖的记录 max(case when CASE ltrim(ps.SIZE) WHEN '4.5' THEN ps.PairPerCarton END is null then null else ps.PairPerCarton end ) AS [4.5]
    为什么margin-top值不是作用域父元素
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5925391.html
Copyright © 2020-2023  润新知