zoukankan      html  css  js  c++  java
  • 设计模式:抽象工厂模式(Abstract Factory)

    定   义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

    结构图

    示例结构图:

    实体类:

     class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    
        class Department
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    View Code

    数据访问接口:

     interface IUser
        {
            void Insert(User user);
    
            User GetUser(int id);
        }
    
        interface IDepartment
        {
            void Insert(Department department);
    
            Department GetDepartment(int id);
        }
    View Code

    User数据库访问具体类:

     class SqlServerUser : IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("向SqlServer数据库中User表添加新用户");
            }
    
            public User GetUser(int id)
            {
                Console.WriteLine("根据用户Id从SqlServer数据库中获取User");
                return null;
            }
        }
    
        class AccessUser : IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("向Access数据库中User表添加新用户");
            }
    
            public User GetUser(int id)
            {
                Console.WriteLine("根据Id从Access数据库中获取User");
                return null;
            }
        }
    View Code

    Department数据库访问具体类:

     class SqlServerDepartment : IDepartment
        {
            public void Insert(Department department)
            {
                Console.WriteLine("向SqlServer数据库Department表中添加一条新记录");
            }
    
            public Department GetDepartment(int id)
            {
                Console.WriteLine("根据Id从SqlServer数据库Department表中部门信息");
                return null;
            }
        }
    
        class AccessDepartment : IDepartment
        {
            public void Insert(Department department)
            {
                Console.WriteLine("向Access数据库Department表中添加一条新记录");
            }
    
            public Department GetDepartment(int id)
            {
                Console.WriteLine("根据Id从Access数据库Department表中获得部门信息");
    
                return null;
            }
        }
    View Code

    抽象工厂接口:

    interface IFactory
        {
            IUser CreateUser();
    
            IDepartment CreateDepartment();
        }
    View Code

    SqlServerFactory类:

     class SqlServerFactory : IFactory
        {
            public IUser CreateUser()
            {
                return new SqlServerUser();
            }
    
    
            public IDepartment CreateDepartment()
            {
                return new SqlServerDepartment();
            }
        }
    View Code

    AccessFactory类:

     class AccessFactory : IFactory
        {
            public IUser CreateUser()
            {
                return new AccessUser();
            }
    
    
            public IDepartment CreateDepartment()
            {
                return new AccessDepartment();
            }
        }
    View Code

    客户端代码:

                User user = new User();
                Department  department=new Department ();
    
                IFactory factory = new SqlServerFactory();
                IUser iu = factory.CreateUser();
                IDepartment idepart = factory.CreateDepartment();
    
                iu.Insert(user);
                iu.GetUser(1);
    
                idepart.Insert(department);
                idepart.GetDepartment(1);
    View Code

    工厂方法模式和抽象工厂模式区别

    反射+配置文件方式修改以上示例

    结构图:

    配置文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add  key="DBType" value="SqlServer"/>
      </appSettings>
    </configuration>

    DataAccess类:

    class DataAccess
        {
            //程序集名称
            private static readonly string AssemblyName = "DesignPatternControl";
            //数据库类型
            private static readonly string db = ConfigurationManager.AppSettings["DBType"];
    
            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);
            }
        }
    View Code

    客户端调用:

     //反射+抽象方法
                User user = new User();
                Department department = new Department();
    
                IUser iu = DataAccess.CreateUser();
                iu.Insert(user);
                iu.GetUser(1);
    
                IDepartment id = DataAccess.CreateDepartment();
                id.Insert(department);
                id.GetDepartment(1);
    View Code
  • 相关阅读:
    搭建DG(data guard),及搭建过程中遇到的一些小问题 高伟
    介绍linux下vi命令的使用
    linux gcc编译器使用
    Linux进程编程介绍
    事件与接口实例讲解 C#
    Linux 2.6内核的编译步骤及模块的动态加载
    C# 各种定时器比较 zz
    linux下增加系统调用
    VirtualBox共享文件夹
    C++程序的单元测试(转贴)
  • 原文地址:https://www.cnblogs.com/qianxingdewoniu/p/3548382.html
Copyright © 2011-2022 走看看