• 设计模式之抽象工厂模式


    抽象工厂模式

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

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

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

  • 相关阅读:
    会话:Cookie、Session
    Response:HTTP响应、重定向、验证码、ServletContext对象
    javabean 深拷贝
    条形码生成工具类
    java zxing 生成条形码和二维吗
    强大的httpClientUtils
    Java处理图片工具类
    生成二维码
    过滤掉map集合中key或value为空的值
    将异常堆栈信息转换成字符串
  • 原文地址:https://www.cnblogs.com/heml/p/4631554.html
Copyright © 2020-2023  润新知