Walden1024

导航

《大话设计模式》学习笔记6:原型模式

  

  

说明:1..NET在System命名空间中提供了ICloneable接口,其中只有唯一的一个方法Clone(),只需要实现这个接口就可以完成原型模式。【即上图中Prototype用ICloneable代替】

   2.MemberwiseClone():创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。 如果字段是值类型的,则对该字段执行逐位复制。 如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。【对于内部的Class对象和数组,则Copy一份地址;而对于其它内置的int/string/enum/struct/object类型,则Copy一份值(参考C# 浅拷贝与深拷贝区别)】

 

一、简历复印示例:

  

1.具体原型类:

    public class Resume:ICloneable
    {
        private string name;
        private string timeArea;
        private string company;

        public Resume(string name)
        {
            this.name = name;
        }
        public void SetWorkExperience(string timeArea,string company)
        {
            this.timeArea = timeArea;
            this.company = company;
        }
        public void Display()
        {
            Console.WriteLine("{0} 工作经历:{1} {2}", name,timeArea, company);
        }
        public object Clone()
        {
            return (object)this.MemberwiseClone();
        }
    }

2.客户端调用:

    class Program
    {
        static void Main(string[] args)
        {
            Resume a = new Resume("a");
            a.SetWorkExperience("1998-2000", "X公司");

            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("1998-2006", "Y公司");

            a.Display();
            b.Display();
        }
    }

二、简历的深复制示例:

  

  说明:参考上图若Resume类中包含WorkExperience类,则在上面的[简历复印示例]中,Resume复制对WorkExperience的复制为浅复制,不同的Resume对象对WorkExperience指向同一个引用。

1.原型包含类:

    public class WorkExperience:ICloneable
    {
        public string WorkDate { get; set; }
        public string Company { get; set; }

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

2.具体原型类:

    public class Resume:ICloneable
    {
        private string name;
        private WorkExperience workExperience;

        public Resume(string name)
        {
            this.name = name;
            workExperience = new WorkExperience();
        }

        //提供Clone方法调用的私有构造函数,以便克隆“工作经历”的数据
        private Resume(WorkExperience workExperience)
        {
            this.workExperience = (WorkExperience)workExperience.Clone();
        }
        public void SetWorkExperience(string workDate,string company)
        {
            workExperience.WorkDate = workDate;
            workExperience.Company = company;
        }
        public void Display()
        {
            Console.WriteLine("{0} 工作经历:{1} {2}", name,workExperience.WorkDate, workExperience.Company);
        }
        public object Clone()
        {
            Resume resume = new Resume(this.workExperience);
            resume.name = this.name;
            return resume;
        }
    }

3.客户端调用:

    class Program
    {
        static void Main(string[] args)
        {
            Resume a = new Resume("a");
            a.SetWorkExperience("1998-2000", "X公司");

            Resume b = (Resume)a.Clone();

            a.Display();
            b.Display();
        }
    }

 

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

posted on 2015-05-11 15:51  Walden1024  阅读(368)  评论(11编辑  收藏  举报