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();
    }
    
  • 相关阅读:
    前端启动摄像头的API
    落谷训练---
    树的遍历 (和) 玩转二叉树 的总结博客
    L2-010 排座位 (并查集)
    最长回文(manacher模板)
    L2-006 树的遍历
    面试题5:从尾到头打印链表
    面试题4:替换空格
    面试题3:二维数组中的查找
    poj 1511(spfa)
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5925391.html
Copyright © 2011-2022 走看看