大话设计模式-原型模式(9)

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

namespace ProtoTypeFactory
{
    abstract class ProtoType
    {
        private string id;

        public string Id
        {
            get { return id; }
        }

        public ProtoType(string id) 
        {
            this.id = id;
        }

        public abstract ProtoType Clone();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProtoTypeFactory
{
    class ConcretePrototype:ProtoType
    {

        public ConcretePrototype(string id) : base(id) 
        {

        }

        public override ProtoType Clone()
        {
            return (ProtoType)this.MemberwiseClone();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProtoTypeFactory
{
    class Resume:ICloneable
    {
        private string name;
        private string sex;
        private string age;
        private WorkExperinece work;

        public Resume(string name)
        {
            this.name = name;
            work = new WorkExperinece();
        }

        private Resume(WorkExperinece work) 
        {
            this.work = (WorkExperinece)work.Clone();
        }

        public void SetPersonalInfo(string sex,string age)
        {
            this.sex = sex;
            this.age = age;
        }

        public void Display() 
        {
            Console.WriteLine("{0} {1} {2}",name,sex,age);
            Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
        }

        public void SetWorkExperinece(string woekdate,string company)
        {
            work.WorkDate = woekdate;
            work.Company = company;
        }

        public object Clone()
        {
            Resume obj = new Resume(this.work);
            obj.name = this.name;
            obj.sex = this.sex;
            obj.age = this.age;
            return obj;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProtoTypeFactory
{
    class WorkExperinece: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();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProtoTypeFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            //原型模式,浅复制
            ConcretePrototype pi = new ConcretePrototype("I");
            ConcretePrototype ci = (ConcretePrototype)pi.Clone();
            Console.WriteLine("Cloned:{0}",ci.Id);

            //原型模式,深度复制
            Resume a = new Resume("大鸟");
            a.SetPersonalInfo("", "29");
            a.SetWorkExperinece("1999-8-1","XX公司");

            Resume b = (Resume)a.Clone();
            b.SetWorkExperinece("1878-2-5","YY企业");

            Resume c = (Resume)a.Clone();
            c.SetPersonalInfo("", "24");
            c.SetWorkExperinece("1978-2-5", "ZZ企业");

            a.Display();
            b.Display();
            c.Display();

            Console.ReadLine();
        }
    }
}

原型模式

  原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.

   原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。

   .net 在system 命名空间中提供了ICloneable 接口,其中就是唯一的一个方法Clone(),实现这个接口就可以完成原型模式了。

   一般在初始化的信息不发生变动的情况下,不用重新初始化对象,而是动态的获得对象运行时的状态,克隆是最好的方法。这既隐藏了对象创建的细节,又对性能是大大的提高。

浅复制与深复制

  MemberwiseClone() 方法是这样,如果字段是值类型的,则对该字段进行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象:因此,原始对象及其复本引用同一对象。

  浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。

  深复制,把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。

原文地址:https://www.cnblogs.com/rinack/p/5280017.html