[設計模式練習]抽象工廠模式

[設計模式練習]抽象工廠模式

當表格不同方式連結(sql or access)利用抽象工廠完成

類別圖

AbstractFactory

物件檔定義


using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace DesignModelTest.抽象工廠
{
    /// <summary>
    /// 員工姓名存取介面
    /// </summary>
    interface IEmployeeName
    {
        void Add(EmployeeName empname);
        EmployeeName GetEmpName(string id);
    }
    /// <summary>
    /// 員工部門名稱存取介面
    /// </summary>
    interface IEmployeeDepName
    {
        void Add(EmployeeDepName empname);
        EmployeeDepName GetEmpDepName(string id);
    }
    /// <summary>
    /// 員工姓名
    /// </summary>
    class EmployeeName
    {
        private string _name;
        private string _id;
        public string name
        {
            get{ return _name;}
            set{ _name = value;}
        }
        public string id
        {
            get { return _id; }
            set { _id = value; }
        }
    }
    /// <summary>
    /// 員工部門名稱
    /// </summary>
    class EmployeeDepName
    {
        private string _name;
        private string _id;
        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string id
        {
            get { return _id; }
            set { _id = value; }
        }
    }
    /// <summary>
    /// 實做員工名稱sqlserver存取
    /// </summary>
    class SqlserverEmployeeName : IEmployeeName
    {
        public void Add(EmployeeName empname)
        {
            Console.WriteLine("在sqlserver給empname表增加記錄");
        }
        public EmployeeName GetEmpName(string id)
        {
            Console.WriteLine("在sqlserver根據ID在empname表取得一筆記錄");
            return null;
        }
    }
    /// <summary>
    /// 實做員工名稱Accessserver存取
    /// </summary>
    class AccessserverEmployeeName : IEmployeeName
    {
        public void Add(EmployeeName empname)
        {
            Console.WriteLine("在Accessserver給empname表增加記錄");
        }
        public EmployeeName GetEmpName(string id)
        {
            Console.WriteLine("在Accessserver根據ID在empname表取得一筆記錄");
            return null;
        }
    }
    /// <summary>
    /// 實做員工部門名稱sqlserver存取
    /// </summary>
    class SqlserverEmployeeDepName : IEmployeeDepName
    {
        public void Add(EmployeeDepName empname)
        {
            Console.WriteLine("在sqlserver給empname表增加記錄");
        }
        public EmployeeDepName GetEmpDepName(string id)
        {
            Console.WriteLine("在sqlserver根據ID在empdepname表取得一筆記錄");
            return null;
        }
    }
    /// <summary>
    /// 實做員工部門名稱Accessserver存取
    /// </summary>
    class AccessserverEmployeeDepName : IEmployeeDepName
    {
        public void Add(EmployeeDepName empname)
        {
            Console.WriteLine("在Accessserver給empdepname表增加記錄");
        }
        public EmployeeDepName GetEmpDepName(string id)
        {
            Console.WriteLine("在Accessserver根據ID在empdepname表取得一筆記錄");
            return null;
        }
    }
    /// <summary>
    /// 用反設及設定檔實現資料存取
    /// </summary>
    class DataAccess
    {
        //組件名稱
        private static readonly string AssemblyName = "DesignModelTest";
        //所選擇的DB
        private static readonly string db = ConfigurationManager.AppSettings["DB"].ToString();
        /// <summary>
        /// 選取要使用的介面
        /// </summary>
        /// <returns></returns>
        public static IEmployeeName CreateName()
        {
            string className = AssemblyName + ".抽象工廠." + db + "EmployeeName";
            //反射 Assembly.Load("程式及名稱").CreateInstance("命名空間.類別名稱");
            return (IEmployeeName)Assembly.Load(AssemblyName).CreateInstance(className);
        }
        public static IEmployeeDepName CreateDepName()
        {
            string className = AssemblyName + ".抽象工廠." + db + "EmployeeDepName";
            return (IEmployeeDepName)Assembly.Load(AssemblyName).CreateInstance(className);
        }
    }
    
}

用戶端程式碼


            DesignModelTest.抽象工廠.EmployeeDepName empdepid = new DesignModelTest.抽象工廠.EmployeeDepName();
            DesignModelTest.抽象工廠.EmployeeName empname = new DesignModelTest.抽象工廠.EmployeeName();
            DesignModelTest.抽象工廠.IEmployeeName Iempname = DesignModelTest.抽象工廠.DataAccess.CreateName();
            Iempname.Add(empname);
            //調用
            Iempname.GetEmpName(string.Empty);
            DesignModelTest.抽象工廠.IEmployeeDepName Iempdepname = DesignModelTest.抽象工廠.DataAccess.CreateDepName();
            Iempdepname.Add(empdepid);
            Iempdepname.GetEmpDepName(string.Empty);
            Console.Read();
            #endregion

輸出結果

pic1