zoukankan      html  css  js  c++  java
  • 设计模式之抽象工厂模式

    抽象工厂模式

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

    User

    package com.hml.factory.abstr;
    
    public class User {
    
    }

    Department

    package com.hml.factory.abstr;
    
    public class Department {
    
    }

    IUser

    package com.hml.factory.abstr;
    
    public interface IUser {
        
        public void insert(User user);
        
        public User getUser(int id);
    }

    IDepartment

    package com.hml.factory.abstr;
    
    public interface IDpartment {
        
        public void insert(Department department);
        
        public Department getDepartment(int id);
    }

    SqlserverUser

    package com.hml.factory.abstr;
    
    public class SqlserverUser implements IUser {
    
        public void insert(User user) {
            System.out.println("SqlserverUser");        
        }
    
        public User getUser(int id) {
            System.out.println("SqlserverUser");
            return null;
        }
    
    }

    SqlserverDepartment

    package com.hml.factory.abstr;
    
    public class SqlserverDepartment implements IDpartment {
    
        public void insert(Department department) {
            System.out.println("SqlserverDepartment");
        }
    
        public Department getDepartment(int id) {
            System.out.println("SqlserverDepartment");
            return null;
        }
    
    }

    OracleUser

    package com.hml.factory.abstr;
    
    public class OracleUser implements IUser {
    
        public void insert(User user) {
            System.out.println("SqlserverUser");        
        }
    
        public User getUser(int id) {
            System.out.println("SqlserverUser");
            return null;
        }
    
    }

    OracleDepartment

    package com.hml.factory.abstr;
    
    public class OracleDepartment implements IDpartment {
    
        public void insert(Department department) {
            System.out.println("OracleDepartment");
        }
    
        public Department getDepartment(int id) {
            System.out.println("OracleDepartment");
            return null;
        }
    
    }

    IFactory

    package com.hml.factory.abstr;
    
    public interface IFactory {
    
        public IUser createUser();
        
        public IDpartment createDepartment();
    }

    SqlserverFactory

    package com.hml.factory.abstr;
    
    public class SqlserverFactory implements IFactory {
    
        public IUser createUser() {
            return new SqlserverUser();
        }
    
        public IDpartment createDepartment() {
            return new SqlserverDepartment();
        }
    
    }

     OracleFatory

    package com.hml.factory.abstr;
    
    public class OracleFactory implements IFactory {
    
        public IUser createUser() {
            return new OracleUser();
        }
    
        public IDpartment createDepartment() {
            return new OracleDepartment();
        }
    
    }

    Test

    package com.hml.factory.abstr;
    
    public class Test {
    
        public static void main(String[] args) {
            
            IFactory factory = new SqlserverFactory(); //  new OracleFactory();
            
            User user = new User();
            
            factory.createUser().insert(user);
            
        }
    }

    类图


    抽象工厂的优点:1、便于交换产品系列,由于具体工厂类,在一个应用中只需在初始化的时候出现一次,这就使得改变一个应用的具体工厂变得非常容易,只需要改变具体工厂即可使用不同的产品配置。2、他让具体创建实例的过程与客户分离,客户端是通过他们的抽象接口操作实例,产品的具体类名也被工厂的实现分离,不会出现在客户代码中。

    该实例可通过配置文件然后再通过反射进行更近一步的封装,这样客户端代码就不用与具体的工厂类交互。

  • 相关阅读:
    vs 2015 "加载该页时出错。" 解决方案
    Web API使用HttpResponseMessage与HttpResponseException的差异 HttpResponseMessage 返回类型
    a标签使用href=”javascript:void(0); 在火狐浏览器跟chrome 不兼容
    SQL语句 转
    Fiddler抓包 截包伪造提交包
    图片懒加载
    dropzone 上传插件
    MVC5+EF6 完整教程 转
    用Aspose.Cell控件导入Excel非强类型的数据
    利用Aspose.Word控件实现Word文档的操作
  • 原文地址:https://www.cnblogs.com/heml/p/4631554.html
Copyright © 2011-2022 走看看