C#反射—解決類的實例化問題

利用簡單工廠模式來改進抽象工廠使用的複雜性(抽象工廠詳見 設計模式之—抽象工廠模式html

數據表(User)業務處理接口(IUser)數據庫

namespace FactoryMethodPatternDB.CLASS
{
    interface IUser
    {
        void Insert(User user);
        void GetUser(string id);
    }
}
View Code

數據表(Department)業務處理接口(IDepartment)設計模式

namespace FactoryMethodPatternDB.CLASS
{
    interface IDepartment
    {
        void Insert(Department dept);
        void GetDept(string id);
    }
}
View Code

SQL操做表(User)類(SqlServerUser)ide

namespace FactoryMethodPatternDB.CLASS
{
    class SqlServerUser:IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在User表中插入一條數據{0}!SQL數據庫",user);
        }

        public void GetUser(string id)
        {
            Console.WriteLine("經過{0}來查找數據!SQL數據庫", id);
        }
    }
}
View Code

SQL操做表(Department)類(SqlServerDepartment)測試

namespace FactoryMethodPatternDB.CLASS
{
    class SqlServerDepartment:IDepartment
    {
        public void Insert(Department dept)
        {
            Console.WriteLine("在{0}表中插入數據!SQL數據庫", dept);
        }
        public void GetDept(string id)
        {
            Console.WriteLine("經過{0}查找數據庫表!SQL數據庫", id);
        }

    }
}
View Code

ACCESS操做表(User)類(AccessUser)spa

namespace FactoryMethodPatternDB.CLASS
{
    class AccessUser:IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在{0}表中插入數據!Access數據庫",user);
        }
        public void GetUser(string id)
        {
            Console.WriteLine("經過{0}查找數據庫表!Access數據庫",id);
        }
    }
}
View Code

ACCESS操做表(Department)類(AccessDepartment)設計

namespace FactoryMethodPatternDB.CLASS
{
    class AccessDepartment:IDepartment
    {
        public void Insert(Department dept)
        {
            Console.WriteLine("在{0}表中插入數據!Access數據庫", dept);
        }
        public void GetDept(string id)
        {
            Console.WriteLine("經過{0}查找數據庫表!Access數據庫", id);
        }
    }
}
View Code

工廠接口和工廠類被SelectDB替換:3d

namespace FactoryMethodPatternDB
{
    class SelectDB
    {
        //private static readonly string db = "Sqlserver";
        private static readonly string db = "Access";
        public static IUser CreateUser()
        {
            IUser iuser = null;
            switch (db)
            {
                case "Sqlserver":
                    iuser = new SqlServerUser();
                    break;
                case "Access":
                    iuser = new AccessUser();
                    break;
            }
            return iuser;
        }

        public static IDepartment CreateDept()
        {
            IDepartment idept = null;
            switch (db)
            {
                case "Sqlserver":
                    idept = new SqlServerDepartment();
                    break;
                case "Access":
                    idept = new AccessDepartment();
                    break;
            }
            return idept;
        }
    }
}
View Code

簡單工廠模式測試類code

      //簡單工廠與抽象工廠模式相結合
          User user = new User();
            Department dept = new Department();
            IUser iuser = SelectDB.CreateUser();
            iuser.Insert(user);
            iuser.GetUser("ID");
            IDepartment idept = SelectDB.CreateDept();
            idept.Insert(dept);
            idept.GetDept("name");
View Code

  

若增長Oracle數據庫,則須要更改Switch語句塊 違背開放-封閉原則。解決此問題的方法是採用反射server

將SelectDB換成反射類(Reflection)

添加反射引用命名空間using System.Reflection;

using System.Reflection;
namespace FactoryMethodPatternDB
{
    class Reflection
    {
        private static readonly string AssemblyName = "FactoryMethodPatternDB"; //程序集
        private static readonly string db = "SqlServer";  //業務類的名稱爲SqlServer + 表名 如(SqlServerUser)方便反射使用

        public static IUser CreateUser()
        {
            string className = AssemblyName + ".CLASS." + db  + "User"; //類名(帶命名空間)
            return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
        }

        public static IDepartment CreateDept()
        {
            string className = AssemblyName + "." + "Department";
            return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
        }
    }
}
View Code

 反射測試類

   //反射與抽象工廠模式相結合
          User user = new User();
            Department dept = new Department();
            IUser iuser = Reflection.CreateUser();
            iuser.Insert(user);
            iuser.GetUser("ID");

            IDepartment idept = Reflection.CreateDept();
            idept.Insert(dept);
            idept.GetDept("Name");
View Code

 

相關文章
相關標籤/搜索