[設計模式練習]建造者模式

[設計模式練習]建造者模式

將結婚過程分成兩種

快樂結婚:相遇,相愛,結婚

傷心結婚:相遇,相愛,結婚,外遇,分手

類別圖

Builder

物件檔定義


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

namespace DesignModelTest.建造者
{
    /// <summary>
    /// 抽象類別:結婚過程
    /// </summary>
    abstract class MarriageProcess
    {
        protected string man;
        protected string woman;
        public MarriageProcess(string manname, string womanname)
        {
            this.man = manname;
            this.woman = womanname;
        }
        public abstract void Meet();//相遇
        public abstract void Love();//相愛
        public abstract void Marriage();//結婚
        public abstract void Affair();//外遇
        public abstract void BreakUp();//分手
        public abstract void ProcessStart();//開始流程
    }
    /// <summary>
    /// 快樂結婚:相遇,相愛,結婚
    /// </summary>
    class HappyMarriage : MarriageProcess
    {
        public HappyMarriage(string manname, string womanname) : base(manname,womanname)
        {
           
        }
        public override void Meet()
        {
            Console.WriteLine(man + "\t快樂的相遇\t" + woman + "\t");
        }
        public override void Love()
        {
            Console.WriteLine(man + "\t快樂的相愛\t" + woman + "\t");
        }
        public override void Marriage()
        {
            Console.WriteLine(man + "\t快樂的結婚\t" + woman + "\t");
        }
        public override void Affair()
        {
            ;//不會發生
        }
        public override void BreakUp()
        {
            ;//不會發生
        }
        public override void ProcessStart()
        {
            this.Meet();
            this.Love();
            this.Marriage();
        }
    }
    /// <summary>
    /// 傷心結婚:相遇,相愛,結婚,外遇,分手
    /// </summary>
    class SadMarriage : MarriageProcess
    {
        public SadMarriage(string manname, string womanname)
            : base(manname, womanname)
        {
          
        }
        public override void Meet()
        {
            Console.WriteLine(man + "\t傷心的相遇\t" + woman + "\t");
        }
        public override void Love()
        {
            Console.WriteLine(man + "\t傷心的相愛\t" + woman + "\t");
        }
        public override void Marriage()
        {
            Console.WriteLine(man + "\t傷心的結婚\t" + woman + "\t");
        }
        public override void Affair()
        {
            Console.WriteLine(woman + "\t發現\t" + man + "\t外遇了!");
        }
        public override void BreakUp()
        {
            Console.WriteLine(man + "\t傷心的分手\t" + woman + "\t");
        }
        public override void ProcessStart()
        {            
            this.Meet();
            this.Love();
            this.Marriage();
            this.Affair();
            this.BreakUp();
        }
    }
    /// <summary>
    /// 指揮結婚過程
    /// </summary>
    class ProcessDirector
    {
        private MarriageProcess mp;
        public ProcessDirector(MarriageProcess mpinput)
        {
            this.mp = mpinput;
        }
        /// <summary>
        /// 開始結婚過程.
        /// </summary>
        public void StartMarriageProcess()
        {
            mp.ProcessStart();            
        }
    }
}

用戶端程式碼


            //要啟用的流程物件
            DesignModelTest.建造者.MarriageProcess mp1 = new DesignModelTest.建造者.HappyMarriage("快樂男", "快樂女");
            DesignModelTest.建造者.MarriageProcess mp2 = new DesignModelTest.建造者.SadMarriage("悲傷男", "悲傷女");
            //將物件指定給指揮者調用
            DesignModelTest.建造者.ProcessDirector processdirector1 = new DesignModelTest.建造者.ProcessDirector(mp1);
            DesignModelTest.建造者.ProcessDirector processdirector2 = new DesignModelTest.建造者.ProcessDirector(mp2);
            //由指揮者根據傳遞物件啟動流程
            processdirector1.StartMarriageProcess();
            processdirector2.StartMarriageProcess();
            Console.Read();
            #endregion

輸出結果

pic1