• 设计模式之—抽象工厂模式<Abstract Factory pattern>


         

    抽象工厂模式处理的是多表业务。工厂方法模式处理的单表的多项业务。

    本例以选择操作数据库(SQL、 ACCESS)为例,解除与数据库的耦合。方便更改数据库的选取

    本例结构图如下:

    工厂接口类(IFactory)

    namespace FactoryMethodPatternDB.CLASS
    {
        interface IFactory
        {
            IUser CreateUser();
            //IDepartment CreateDept();
        }
    }
    View Code

    SQL操作User表的工厂(SqlServerFactory):继承于工厂接口类(IFactory)

    namespace FactoryMethodPatternDB.CLASS
    {
        class SqlServerFactory:IFactory
        {
            public IUser CreateUser()
            {
                return new SqlServerUser();
            }
        }
    }
    View Code

    ACCESS操作User表的工厂(AccessFactory):继承于工厂接口类(IFactory)

    namespace FactoryMethodPatternDB.CLASS
    {
        class AccessFactory:IFactory
        {
            public IUser CreateUser()
            {
                return new AccessUser();
            }
        }
    }
    View Code

    用户表(User)接口类(IUser)

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

    SQL处理User表的业务(SqlServerUser):继承于用户表接口(IUser)

    namespace FactoryMethodPatternDB.CLASS
    {
        class SqlServerUser:IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("在User表中插入一条数据{0}!",user);
            }
    
            public void GetUser(string id)
            {
                Console.WriteLine("通过{0}来查找数据!",id);
            }
        }
    }
    View Code

    ACCESS处理User表的业务(AccserverUser):继承于用户表接口(IUser)

    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

    封装User表属性:

    namespace FactoryMethodPatternDB.CLASS
    {
        class User
        {
            private string id;
            private User user;
    
            /// <summary>
            /// 用户表对象
            /// </summary>
            public User User1
            {
                get { return user; }
                set { user = value; }
            }
            /// <summary>
            /// ID号
            /// </summary>
            public string Id
            {
                get { return id; }
                set { id = value; }
            }
        }
    }
    View Code

    测试类(TestMain)

    namespace FactoryMethodPatternDB
    {
        class TestMain
        {
            static void Main(string[] args)
            {
                //User user = new User();
                //IFactory sqlFactory = new SqlServerFactory();
                //IUser iUser = sqlFactory.CreateUser();
    
                //iUser.Insert(user);
                //user.Id = "name";
                //iUser.GetUser(user.Id);
    
                User user = new User();
                IFactory accFactory = new AccessFactory();
                IUser iUser = accFactory.CreateUser();
                iUser.Insert(user);
                iUser.GetUser("ID");
    
                Console.ReadLine();
            }
        }
    }
    View Code

    上述为工厂方法模式的实现:抽象方法模式即在此基础上再加入其他表的业务逻辑操作

    在上述的 工厂接口类(IFactory)里添加部门表的(Department)方法即

    namespace FactoryMethodPatternDB.CLASS
    {
        interface IFactory
        {
            IUser CreateUser();
    
            IDepartment CreateDept();
        }
    }
    View Code

    更改SQL操作Department表的工厂、ACCESS操作Dapartment表的工厂

    SQL操作工厂(SqlServerFactory):继承于工厂接口类(IFactory)

    namespace FactoryMethodPatternDB.CLASS
    {
        class SqlServerFactory:IFactory
        {
            public IUser CreateUser()
            {
                return new SqlServerUser();
            }
    
            public IDepartment CreateDepart()
            {
                return new SqlServerDepartment();
            }
        }
    }
    View Code

    ACCESS操作工厂(AccessFactory):继承于工厂接口类(IFactory)

    namespace FactoryMethodPatternDB.CLASS
    {
        class AccessFactory:IFactory
        {
            public IUser CreateUser()
            {
                return new AccessUser();
            }
    
            public IDepartment CreateDepart()
            {
                return new AccessDepart();
            }
        }
    }
    View Code

    添加部门表(Department)的接口,

    添加SQL处理Department表业务的方法、ACCESS处理Department表业务的方法

    测试类中: IFactory sqlFactory = new Sql

    要么忍,要么狠,要么滚!
  • 相关阅读:
    JavaScript学习笔记--this全面解析
    CSS3学习笔记--animation和transition
    HTML5学习笔记--HTML5新增的常用标签
    布局学习笔记--flex布局
    布局学习笔记--rem布局
    HTML5学习笔记--Canvas
    react-pdf插件实现pdf预览功能
    postman基础使用——调试接口
    react 实现复制copy文本内容
    react 移动端签名实现
  • 原文地址:https://www.cnblogs.com/zxd543/p/3250235.html
Copyright © 2020-2023  润新知