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、他让具体创建实例的过程与客户分离,客户端是通过他们的抽象接口操作实例,产品的具体类名也被工厂的实现分离,不会出现在客户代码中。

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

  • 相关阅读:
    设计模式-单例模式
    Java对象声明时:new与null的区别
    Mysql的UseAffectedRows问题 以及其他常见配置说明
    将form表单元素的值序列化成对象
    idea教程视频以及常用插件整理
    Ajax获取Response头信息
    BootstrapTable返回结果集递增序号
    mybaitis动态sql利用bind标签代替%拼接完成模糊查询
    Numpy库使用
    基于socket实现websocket服务
  • 原文地址:https://www.cnblogs.com/heml/p/4631554.html
Copyright © 2011-2022 走看看