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();
    }
    
  • 相关阅读:
    js history对象 手机物理返回键
    正则表达式学习整理
    js获得时间new Date()整理
    币值转换
    打印沙漏
    秋季总结
    对我影响最大的三位老师
    我就是我
    秋季学期学习总结
    自我介绍
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5925391.html
Copyright © 2011-2022 走看看