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

设计模式 抽象工厂模式

时间:2014-12-14 10:36:32      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   os   使用   sp   on   

抽象工厂模式(Abstract Factory)的定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。

可见,抽象工厂模式是指当有多个抽象角色时,使用的一种工厂方法模式。工厂方法模式是针对一个产品等级结构,而抽象工厂是针对多个产品等级结构。

bubuko.com,布布扣
    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; }
        }
    }

    /// <summary>
    /// 抽象产品A
    /// </summary>
    interface IUser
    {
        void Insert(User user);

        User GetUser(int id);
    }

    /// <summary>
    /// 抽象产品B
    /// </summary>
    interface IDepartment
    {
        void Insert(Department department);

        Department GetDepartment(int id);
    }
抽象产品
bubuko.com,布布扣
    /// <summary>
    /// 具体产品A1 抽象产品A的具体分类的实现
    /// </summary>
    class SqlserverUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Sqlserver中给User表增加一条记录");
        }

        public User GetUser(int id)
        {
            Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");
            return null;
        }
    }

    /// <summary>
    /// 具体产品A2 抽象产品A的具体分类的实现
    /// </summary>
    class AccessUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Access中给User表增加一条记录");
        }

        public User GetUser(int id)
        {
            Console.WriteLine("在Access中根据ID得到User表一条记录");
            return null;
        }
    }

    /// <summary>
    /// 具体产品B1 抽象产品B的具体分类的实现
   /// </summary>
    class SqlserverDepartment : IDepartment
    {
        public void Insert(Department department)
        {
            Console.WriteLine("在Sqlserver中给Department表增加一条记录");
        }

        public Department GetDepartment(int id)
        {
            Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");
            return null;
        }
    }

    /// <summary>
    /// 具体产品B2 抽象产品B的具体分类的实现
   /// </summary>
    class AccessDepartment : IDepartment
    {
        public void Insert(Department department)
        {
            Console.WriteLine("在Access中给Department表增加一条记录");
        }

        public Department GetDepartment(int id)
        {
            Console.WriteLine("在Access中根据ID得到Department表一条记录");
            return null;
        }
    }
具体产品
bubuko.com,布布扣
    /// <summary>
    /// 抽象工厂
    /// </summary>
    interface IFactory
    {
        IUser CreateUser();

        IDepartment CreateDepartment();
    }
抽象工厂
bubuko.com,布布扣
    class SqlServerFactory : IFactory
    {
        public IUser CreateUser()
        {
            return new SqlserverUser();
        }

        public IDepartment CreateDepartment()
        {
            return new SqlserverDepartment();
        }
    }

    class AccessFactory : IFactory
    {
        public IUser CreateUser()
        {
            return new AccessUser();
        }

        public IDepartment CreateDepartment()
        {
            return new AccessDepartment();
        }
    }
具体工厂
bubuko.com,布布扣
        static void Main(string[] args)
        {
            User user = new User();
            Department dept = new Department();

            //AbstractFactory factory = new SqlServerFactory();
            IFactory factory = new AccessFactory();
            IUser iu = factory.CreateUser();

            iu.Insert(user);
            iu.GetUser(1);

            IDepartment id = factory.CreateDepartment();
            id.Insert(dept);
            id.GetDepartment(1);
        }
抽象工厂模式的实现

 

设计模式 抽象工厂模式

标签:style   blog   http   ar   color   os   使用   sp   on   

原文地址:http://www.cnblogs.com/YuanSong/p/4162263.html

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