2011/07/03

[Design Patterns] 原型模式(Prototype)

型態:生成模式(CreationalPattern)。
定義:用原型實例指定建立物件的種類,並且透過拷貝這些原型建立新的物件。

結構圖:來源
2011-7-3 下午 05-17-23


原型模式其實就是從一個物件再建立另外一個可訂製的物件,而且不須知道任何建立的細節。
其中有區分為「淺複製」與「深複製」
淺複製:複製所有欄位,參考型別只複製參考。
深複製:建立新的物件。

先來看一下「淺複製」

Resume 類別 (履歷)
//簡歷類別,實作 ICloneable 介面
    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}", name, sex, age);
            Console.WriteLine("工作經歷:{0} {1}", work.WorkDate, work.Company);
        }

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

    }

 

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; }
        }
    }

 

前端調用
class Program
    {
        static void Main(string[] args)
        {
            Resume a = new Resume("大鳥");
            a.SetPersonalInfo("男", "29");
            a.SetWorkExperience("1998-2000", "XX公司");

            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("1998-2006", "YY企業");

            Resume c = (Resume)a.Clone();
            c.SetPersonalInfo("男", "24");
            c.SetWorkExperience("1998-2003", "ZZ企業");

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

            Console.Read();
        }
    }

 

輸出
2011-07-03_174458

上面例子,
我們希望顯示三個不同的工作經歷,可惜沒有達到。

所以我們這時候來借重「深複製」

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; }
        }

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

 

Resume 類別 (履歷)
//簡歷
    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}", name, sex, age);
            Console.WriteLine("工作經歷:{0} {1}", work.WorkDate, work.Company);
        }

        public Object Clone()
        {
            Resume obj = new Resume(this.work);

            obj.name = this.name;
            obj.sex = this.sex;
            obj.age = this.age;


            return obj;
        }

    }


前端調用

Program 類別
 class Program
    {
        static void Main(string[] args)
        {
            Resume a = new Resume("大鳥");
            a.SetPersonalInfo("男", "29");
            a.SetWorkExperience("1998-2000", "XX公司");

            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("1998-2006", "YY企業");

            Resume c = (Resume)a.Clone();
            c.SetWorkExperience("1998-2003", "ZZ企業");

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

            Console.Read();

        }
    }


輸出
2011-07-03_175037

0 Comments:

張貼留言