码迷,mamicode.com
首页 > 其他好文 > 详细

設計模式--抽象工廠模式

时间:2016-03-11 13:44:52      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

抽象工廠用例圖

技术分享

代碼

    class Program

    {

        static void Main(string[] args)

        {

            User user = new User();

            Department dept = new Department();

 

            IUser iu = DataAccess.CreateUser();

 

            iu.Insert(user);

            iu.GetUser(1);

 

            IDepartment id = DataAccess.CreateDepartment();

            id.Insert(dept);

            id.GetDepartment(1);

 

            Console.Read();

        }

    }

 

    class User

    {

        private int _id;

        public int ID

        {

            get { return _id; }

            set { _id = value; }

        }

 

        private string _name;

        public string Name

        {

            get { return _name; }

            set { _name = value; }

        }

    }

 

    class Department

    {

        private int _id;

        public int ID

        {

            get { return _id; }

            set { _id = value; }

        }

 

        private string _deptName;

        public string DeptName

        {

            get { return _deptName; }

            set { _deptName = value; }

        }

    }

 

    interface IUser

    {

        void Insert(User user);

 

        User GetUser(int id);

    }

 

    class SqlserverUser : IUser

    {

        public void Insert(User user)

        {

            Console.WriteLine("在Sqlserver中给User表增加一条记录");

        }

 

        public User GetUser(int id)

        {

            Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");

            return null;

        }

    }

 

    class AccessUser : IUser

    {

        public void Insert(User user)

        {

            Console.WriteLine("在Access中给User表增加一条记录");

        }

 

        public User GetUser(int id)

        {

            Console.WriteLine("在Access中根据ID得到User表一条记录");

            return null;

        }

    }

 

    interface IDepartment

    {

        void Insert(Department department);

 

        Department GetDepartment(int id);

    }

 

    class SqlserverDepartment : IDepartment

    {

        public void Insert(Department department)

        {

            Console.WriteLine("在Sqlserver中给Department表增加一条记录");

        }

 

        public Department GetDepartment(int id)

        {

            Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");

            return null;

        }

    }

 

    class AccessDepartment : IDepartment

    {

        public void Insert(Department department)

        {

            Console.WriteLine("在Access中给Department表增加一条记录");

        }

 

        public Department GetDepartment(int id)

        {

            Console.WriteLine("在Access中根据ID得到Department表一条记录");

            return null;

        }

    }

 

    class DataAccess

    {

        private static readonly string AssemblyName = "抽象工厂模式";

        private static readonly string db = ConfigurationManager.AppSettings["DB"];

       

        public static IUser CreateUser()

        {

            string className = AssemblyName + "." + db + "User";

            return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);

        }

 

        public static IDepartment CreateDepartment()

        {

            string className = AssemblyName + "." + db + "Department";

            return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);

        }

    }

 

運行效果

 技术分享

設計模式--抽象工廠模式

标签:

原文地址:http://www.cnblogs.com/sdya/p/5265061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!