[設計模式練習] 簡單工廠及策略模式

[設計模式練習] 簡單工廠及策略模式

最近在讀大話設計模式

將範例改寫練習一下

類別圖

SimpleFactory

物件檔定義

 

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

namespace DesignModelTest.簡單工廠
{
    [Flags]
    public enum JoinOption
    {
        內部結合 = 1,
        外部結合 = 2,
        左結合 = 3,
        右結合 = 4
    };    
    interface IResultTest
    {
        string ShowResult();
    }
    /// <summary>
    /// 結合種類
    /// </summary>
    public class JoinTypes
    {
        private string _tableLeftSide;
        /// <summary>
        /// 左表名稱.
        /// </summary>
        /// <value>The table left side.</value>
        public string tableLeftSide
        {
            get { return _tableLeftSide; }
            set { _tableLeftSide = value; }
        }
        private string _tableRightSide;
        /// <summary>
        /// 右表名稱.
        /// </summary>
        /// <value>The table right side.</value>
        public string tableRightSide
        {
            get { return _tableRightSide; }
            set { _tableRightSide = value; }
        }
        /// <summary>
        /// 設定表格名稱.
        /// </summary>
        /// <param name="tableLeftName">Name of the table left.</param>
        /// <param name="tableRightName">Name of the table right.</param>
        public void SetTableNames(string tableLeftName, string tableRightName)
        {
            tableLeftSide = string.Copy(tableLeftName);
            tableRightSide = string.Copy(tableRightName);
        }
        /// <summary>
        /// 得到結果.
        /// </summary>
        /// <returns></returns>
        public virtual string GetResult()
        {
            return tableLeftSide + "\t" + tableRightSide;
        }       

    }
    public class InnerJoin : JoinTypes,IResultTest
    {
       
        public override string GetResult()
        {
            return this.tableLeftSide + " Inner Join " + this.tableRightSide;
        }
        public string ShowResult()
        {
            return "InnerJoin ok!";
        }
    }
    public class OuterJoin : JoinTypes, IResultTest
    {
         
        public override string GetResult()
        {
            return this.tableLeftSide + " Outer Join " + this.tableRightSide;
        }
        public string ShowResult()
        {
            return "OuterJoin ok!";
        }
    }
    public class LeftJoin : JoinTypes, IResultTest
    {
        public override string GetResult()
        {
            return this.tableLeftSide + " Left Join " + this.tableRightSide;
        }
        public string ShowResult()
        {
            return "LeftJoin ok!";
        }
    }
    public class RightJoin : JoinTypes, IResultTest
    {
        public override string GetResult()
        {
            return this.tableLeftSide + " Right Join " + this.tableRightSide;
        }
        public string ShowResult()
        {
            return "RightJoin ok!";
        }
    }
    /// <summary>
    /// 簡單工廠:管理結合種類的子類別
    /// </summary>
    public class JoinTypesFactory
    {
        /// <summary>
        /// Chooses the type of the join.
        /// </summary>
        /// <param name="Option">The option.</param>
        /// <returns></returns>
        public static JoinTypes ChooseJoinType(JoinOption Option)
        {
            JoinTypes selectType = null;
            switch(Option.ToString())
            {
                case "內部結合" : selectType = new InnerJoin();
                    break;
                case "外部結合" : selectType = new OuterJoin();
                    break;
                case "左結合" : selectType = new LeftJoin();
                    break;
                case "右結合" : selectType = new RightJoin();
                    break;
                default: break;
            }
            return selectType;
        }
    }

    /// <summary>
    /// 策略與簡單工廠結合
    /// </summary>
    class StrategyFactory
    {
        JoinTypes selectType = null;
        public StrategyFactory(JoinOption option, string leftTableName, string rightTableName)
        {
            switch (option.ToString())
            {
                case "內部結合": JoinTypes s1 = new InnerJoin();
                    selectType = s1;
                    break;
                case "外部結合": JoinTypes s2 = new OuterJoin();
                    selectType = s2;
                    break;
                case "左結合": JoinTypes s3 = new LeftJoin();
                    selectType = s3;
                    break;
                case "右結合": JoinTypes s4 = new RightJoin();
                    selectType = s4;

                    break;
                default: break;
            }
            selectType.tableLeftSide = string.Copy(leftTableName);
            selectType.tableRightSide = string.Copy(rightTableName);
        }
        public string GetResult()
        {
            return selectType.GetResult();
        }
    }
}

用戶端程式碼

            DesignModelTest.簡單工廠.JoinTypes selectJoinType;
            //經由簡單工廠的函式將子類別實體化
            selectJoinType = DesignModelTest.簡單工廠.JoinTypesFactory.ChooseJoinType(DesignModelTest.簡單工廠.JoinOption.外部結合);
            selectJoinType.SetTableNames("左表", "右表");
            //取得結果
            Console.WriteLine(selectJoinType.GetResult());
            Console.Read();
            #endregion

結果

pic1

用戶端程式碼

            DesignModelTest.簡單工廠.StrategyFactory sfactory = new StrategyFactory(DesignModelTest.簡單工廠.JoinOption.右結合, "左表", "右表");
            //DesignModelTest.簡單工廠.StrategyFactory sfactory = new StrategyFactory(StrategyOption.演算法一);//基本用法            
            Console.WriteLine(sfactory.GetResult());
            Console.Read();
            #endregion

結果

pic2