定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象

浅拷贝:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 原型模式
{
    class Program
    {
        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; }
            }
        }
        class Resume:ICloneable
        {
            private string name;
            private string sex;
            private string age;
            private WorkExperience work;
            public Resume(string name)
            {
                this.name = name;
                work = new WorkExperience();
            }
            public void SetPersonalInfo(string sex,string age)
            {
                this.sex = sex;
                this.age = age;
            }
            public void SetWorkExperience(string workDate,string company)
            {
                work.WorkDate = workDate;
                work.Company = company;
            }
            public void Display()
            {
                Console.WriteLine("{0} {1} {2} {3} {4}", name, sex, age, work.Company, work.WorkDate);
            }
            public Object Clone()
            {
                return (Object)this.MemberwiseClone();
            }
        }
        static void Main(string[] args)
        {
            Resume a = new Resume("misszhou");
            a.SetPersonalInfo("miss", "23");
            a.SetWorkExperience("2014-2015", "nefu");
            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("2015-2016", "w");
            a.Display();
            b.Display();
            Console.Read();
        }
    }
}
输出:misszhou miss 23 w 2015-2016

misszhou miss 23 w 2015-2016

深拷贝

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 原型模式
{
    class Program
    {
        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; }
            }
            public Object Clone()
            {
                return (Object)this.MemberwiseClone();
            }
        }
        class Resume:ICloneable
        {
            private string name;
            private string sex;
            private string age;
            private WorkExperience work;
            public Resume(string name)
            {
                this.name = name;
                work = new WorkExperience();
            }
            private Resume(WorkExperience work)
            {
                this.work = (WorkExperience)work.Clone();
            }
            public void SetPersonalInfo(string sex,string age)
            {
                this.sex = sex;
                this.age = age;
            }
            public void SetWorkExperience(string workDate,string company)
            {
                work.WorkDate = workDate;
                work.Company = company;
            }
            public void Display()
            {
                Console.WriteLine("{0} {1} {2} {3} {4}", name, sex, age, work.Company, work.WorkDate);
            }
            public Object Clone()
            {
                Resume obj = new Resume(this.work);
                obj.name = this.name;
                obj.sex = this.sex;
                obj.age = this.age;
                return obj;
            }
        }
        static void Main(string[] args)
        {
            Resume a = new Resume("misszhou");
            a.SetPersonalInfo("miss", "23");
            a.SetWorkExperience("2014-2015", "nefu");
            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("2015-2016", "w");
            a.Display();
            b.Display();
            Console.Read();
        }
    }
}