原型模式(Prototype Pattern)

馋奶兔 提交于 2020-03-20 08:11:04
 

原型模式(Prototype Pattern

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。模式的本质就是不断重复出现的问题的可重用解决方案。

(1)       原型实现

Person

    public class Person:ICloneable

    {

        private string name;

        private string sex;

        private string age;

        private string timeArea;

        private string company;

 

        public Person(string name)

        {

            this.name = name;

        }

 

        public void SetPersonInfo(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 Show()

        {

            Console.WriteLine("{0},{1},{2}",name,sex,age);

            Console.WriteLine("工作经历:{0},{1}",timeArea,company);

        }

 

        #region ICloneable 成员

 

        public object Clone()

        {

            return (object)this.MemberwiseClone();

        }

 

        #endregion

    }

客户端代码

        static void Main(string[] args)

        {

            Person person = new Person("呆呆");

            person.SetPersonInfo("男", "23");

            person.SetWorkExperience("2008-7", "视通");

 

            Person b = (Person)person.Clone();

            b.SetWorkExperience("2005-09","HQU");

 

            Person c = (Person)person.Clone();

            c.SetWorkExperience("2009-07", "毕业");

 

            person.Show();

            b.Show();

            c.Show();

            Console.Read();

        }

运行结果:

(2)浅复制

Person类:

public class Person:ICloneable

    {

        private string name;

        private string sex;

        private string age;

        private WorkExperience workExperience;

 

        public Person(string name)

        {

            this.name = name;

            workExperience = new WorkExperience();

        }

 

        public void SetPersonInfo(string sex,string age)

        {

            this.sex = sex;

            this.age = age;

        }

 

        public void SetWorkExperience(string timeArea, string company)

        {

            workExperience.workDate = timeArea;

            workExperience.company = company;

        }

 

        public void Show()

        {

            Console.WriteLine("{0},{1},{2}",name,sex,age);

            Console.WriteLine("工作经历:{0},{1}", workExperience.workDate, workExperience.company);

        }

 

        #region ICloneable 成员

 

        public object Clone()

        {

            return (object)this.MemberwiseClone();

        }

 

        #endregion

}

 

WorkExperience

 class WorkExperience

    {

        private string _workDate;

        public string workDate

        {

            get { return _workDate; }

            set { _workDate = value; }

        }

 

        private string _company;

        public string company

        {

            get { return _company; }

            set { _company = value; }

        }

}

 

客户端实现与前面一样。

运行结果:

 

(3)深复制

WorkExperience

    class WorkExperience:ICloneable

    {

        private string _workDate;

        public string workDate

        {

            get { return _workDate; }

            set { _workDate = value; }

        }

 

        private string _company;

        public string company

        {

            get { return _company; }

            set { _company = value; }

        }

 

        #region ICloneable 成员

 

        public object Clone()

        {

            return (object)this.MemberwiseClone();

        }

 

        #endregion

}

 

Person类:

    public class Person:ICloneable

    {

        private string name;

        private string sex;

        private string age;

        private WorkExperience workExperience;

 

        public Person(string name)

        {

            this.name = name;

            workExperience = new WorkExperience();

        }

 

        private Person(WorkExperience workExperience)

        {

            this.workExperience = (WorkExperience)workExperience.Clone();

        }

 

        public void SetPersonInfo(string sex,string age)

        {

            this.sex = sex;

            this.age = age;

        }

 

        public void SetWorkExperience(string timeArea, string company)

        {

            workExperience.workDate = timeArea;

            workExperience.company = company;

        }

 

        public void Show()

        {

            Console.WriteLine("{0},{1},{2}",name,sex,age);

            Console.WriteLine("工作经历:{0},{1}", workExperience.workDate, workExperience.company);

        }

 

        #region ICloneable 成员

 

        public object Clone()

        {

            Person obj = new Person(this.workExperience);

            obj.name = this.name;

            obj.sex = this.sex;

            obj.age = this.age;

            return obj;

        }

 

        #endregion

    }

 

客户段代码与之前一样

运行结果:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!