設計模式學習筆記——原型模式

來源:互聯網
上載者:User

標籤:

1.特點:在初始化資訊不發生變化時考慮。

2.概念:通過複製一個已經存在的執行個體來建立一個新的執行個體。被複製的執行個體被稱為原型,這個原型是可定製的。

3.類圖:

4.程式實現:

/// <summary>    /// 實現了ICloneable介面的簡曆類    /// </summary>    public class Resume:ICloneable    {        public Resume()        {            mWorkExperience = new WorkExperience();        }        private string mName;        private string mSex;        private int mAge;        private WorkExperience mWorkExperience;        public string Name        {            get { return mName; }            set { mName = value; }        }        public string Sex        {            get { return mSex; }            set { mSex = value; }        }        public int Age        {            get { return mAge; }            set { mAge = value; }        }        /// <summary>        /// 關聯了一個參考型別        /// </summary>        public WorkExperience WorkExperience         {             get { return mWorkExperience; }         }        public void SetWorkExperience(DateTime startDate, DateTime endDate, string company, string position)        {            this.mWorkExperience.Company = company;            this.mWorkExperience.EndDate = endDate;            this.mWorkExperience.StartDate = startDate;            this.mWorkExperience.Position = position;        }        /// <summary>        /// 實現ICloneable介面的Clone方法        /// </summary>        /// <returns></returns>        public object Clone()        {            // .Net 為我們提供的淺複製對象的方法            return this.MemberwiseClone();        }    }    /// <summary>    /// 工作經曆類    /// </summary>    public class WorkExperience    {        public DateTime StartDate { get; set; }        public DateTime EndDate { get; set; }        public string Company { get; set; }        public string Position { get; set; }    }[TestMethod]        public void TestShallowCopy()        {            Resume myFirstResume = new Resume            {                Age = 29,                Name = "Kevin Wang",                Sex = "男",            };            myFirstResume.SetWorkExperience(new DateTime(2006, 7, 1), new DateTime(2007, 7, 1), "My First Company", "Software Engineer");            Resume mySecondResume = (Resume)myFirstResume.Clone();            mySecondResume.SetWorkExperience(new DateTime(2007, 8, 1), new DateTime(2008, 8, 1), "My Second Company", "Software Engineer");            Resume myThirdResume = (Resume)myFirstResume.Clone();            myThirdResume.SetWorkExperience(new DateTime(2008, 8, 1), new DateTime(2009, 8, 1), "My Third Company", "Senior Software Engineer");            Assert.AreEqual("My First Company", myFirstResume.WorkExperience.Company);            Assert.AreEqual("My Second Company", mySecondResume.WorkExperience.Company);            Assert.AreEqual("My Third Company", myThirdResume.WorkExperience.Company);               }

  

設計模式學習筆記——原型模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.