设计模式之—原型模式<Prototype Pattern>

原型模式就是从一个对象在创建另外一个对象,不需要知道任何创建的细节:(克隆)

以创建简历为例:

简历类(Resume) :继承于系统的克隆接口(ICloneable)

namespace Ptototype_Pattern
{
    class ReSume:ICloneable
    {
        private string name;
        private string age;
        private string sex;
        private string birthday;
        private string time;
        private string company;
        public ReSume(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// 设置个人信息
        /// </summary>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        public void SetPersonInfo(string age, string sex, string birthday)
        {
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
        }

        /// <summary>
        /// 设置工作经历
        /// </summary>
        /// <param name="time"></param>
        /// <param name="company"></param>
        public void SetWorkExperice(string time, string company)
        {
            this.time = time;
            this.company = company;
        }

        public void Display()
        {
            Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            Console.WriteLine("在 {0} 工作于 {1} \n",time,company);
        }

        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }
}
View Code

客户端类(TestMain)

namespace Ptototype_Pattern
{
    class TestMain
    {
        static void Main(string[] args)
        {
            ReSume A = new ReSume("张三");
            A.SetPersonInfo("25","","1988-10-10");
            A.SetWorkExperice("2010--2013", "X公司");

            ReSume B = (ReSume)A.Clone();
            B.SetWorkExperice("2009--2010", "Y公司");

            ReSume C = (ReSume)A.Clone();
            C.SetWorkExperice("2006--2009", "Z公司");

            A.Display();
            B.Display();
            C.Display();

            Console.ReadLine();

        }
    }
}
View Code

 

深复制和浅复制:MemberwiseClone 方法创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。 如果字段是值类型的,则对该字段执行逐位复制。 如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。

引用类型的例子如下:

新添加工作经历类(WorkExperice)

 

namespace Ptototype_Pattern
{
    class WorkExperice
    {
        private string workTime;
        private string workCompany;
        /// <summary>
        /// 工作时间
        /// </summary>
        public string WorkTime
        {
            get { return workTime; }
            set { workTime = value; }
        }

        /// <summary>
        /// 工作企业
        /// </summary>
        public string WorkCompany
        {
            get { return workCompany; }
            set { workCompany = value; }
        }
    }
}
View Code

 

简历类(Resume) :继承于系统的克隆接口(ICloneable)

namespace Ptototype_Pattern
{
    class ReSume:ICloneable
    {
        private string name;
        private string age;
        private string sex;
        private string birthday;
        //private string time;
        //private string company;
        private WorkExperice work;
        public ReSume(string name)
        {
            this.name = name;
            work = new WorkExperice();

        }

        /// <summary>
        /// 设置个人信息
        /// </summary>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        public void SetPersonInfo(string age, string sex, string birthday)
        {
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
        }

        /// <summary>
        /// 设置工作经历
        /// </summary>
        /// <param name="time"></param>
        /// <param name="company"></param>
        public void SetWorkExperice(string time, string company)
        {
            //this.time = time;
            //this.company = company;
            work.WorkTime = time;
            work.WorkCompany = company;

        }

        public void Display()
        {
            //Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            //Console.WriteLine("在 {0} 工作于 {1} \n",time,company);

            Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            Console.WriteLine("在 {0} 工作于 {1} \n", work.WorkTime, work.WorkCompany);

        }

        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }
}
View Code

客户端不变:(全为Z公司)

MemberwiseClone 方法创建一个浅表副本,对于引用类型复制引用,但不复制引用的对象

改为深复制如下:

工作经历类(WorkExperice)

 

namespace Ptototype_Pattern
{
    class WorkExperice:ICloneable
    {
        private string workTime;
        private string workCompany;
        /// <summary>
        /// 工作时间
        /// </summary>
        public string WorkTime
        {
            get { return workTime; }
            set { workTime = value; }
        }

        /// <summary>
        /// 工作企业
        /// </summary>
        public string WorkCompany
        {
            get { return workCompany; }
            set { workCompany = value; }
        }

        public Object Clone()
        {
            return (object)this.MemberwiseClone();
        }
    }
}
View Code

 

简历类(Resume) :继承于系统的克隆接口(ICloneable)

namespace Ptototype_Pattern
{
    class ReSume:ICloneable
    {
        private string name;
        private string age;
        private string sex;
        private string birthday;
        //private string time;
        //private string company;
        private WorkExperice work;
        public ReSume(string name)
        {
            this.name = name;
            work = new WorkExperice();

        }
        public ReSume(WorkExperice work)
        {
            this.work = (WorkExperice)work.Clone();
        }

        /// <summary>
        /// 设置个人信息
        /// </summary>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        public void SetPersonInfo(string age, string sex, string birthday)
        {
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
        }

        /// <summary>
        /// 设置工作经历
        /// </summary>
        /// <param name="time"></param>
        /// <param name="company"></param>
        public void SetWorkExperice(string time, string company)
        {
            //this.time = time;
            //this.company = company;
            work.WorkTime = time;
            work.WorkCompany = company;

        }

        public void Display()
        {
            //Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            //Console.WriteLine("在 {0} 工作于 {1} \n",time,company);

            Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            Console.WriteLine("在 {0} 工作于 {1} \n", work.WorkTime, work.WorkCompany);

        }

        public Object Clone()
        {
            //return (Object)this.MemberwiseClone();
            ReSume resum = new ReSume(this.work);
            resum.name = this.name;
            resum.age = this.age;
            resum.birthday = this.birthday;
            return resum;
        }
    }
}
View Code

客户端不变:

 

 

 

 

 

 

转载于:https://www.cnblogs.com/zxd543/p/3244788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值