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

    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();
    }
    
  • 相关阅读:
    模拟+bfs——cf1344D
    【留坑】模拟+极角排序+预处理——ICPC PNWRC 2019 H
    【好题】思维+几何+离散化——ICPC PNWRC 2019 G
    【难】组合数学+dp——ICPC PNWRC 2019
    【好题】导数+统计贡献+扫描—— icpc PNWRC 2019
    【模板】二分图匹配+构造+最大独立集——icpc PNWRC 2019
    [Scoi2014]方伯伯的OJ(动态开点splay)
    [ZJOI2006]书架(二分+树状数组)
    [TJOI2017]不勤劳的图书管理员(分块+树状数组)
    [APIO2016]Gap(交互)
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5925391.html
Copyright © 2011-2022 走看看