美文网首页
设计模式之抽象工厂模式

设计模式之抽象工厂模式

作者: 正在加载更多 | 来源:发表于2018-09-05 08:35 被阅读0次
    定义

    抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

    角色
    • 抽象产品类A
    • 抽象产品类B
    • 抽象工厂类
    • 具体工厂类C(生成A)
    • 具体工厂类D(生产B)
    UML类图
    抽象工厂模式UML类图
    代码
    • IBenz.java
    public interface IBenz {
        void run();
    }
    
    • IBWM.java
    public interface IBWM {
        void run();
    }
    
    • HomeUseBenz.java
    public class HomeUseBenz implements IBenz {
        @Override
        public void run() {
            System.out.println("first.HomeUseBenz run");
        }
    }
    
    • HomeUseBWM.java
    public class HomeUseBWM implements IBWM {
        @Override
        public void run() {
            System.out.println("first.HomeUseBWM run");
        }
    }
    
    • BusinessUseBenz.java
    public class BusinessUseBenz implements IBenz {
        @Override
        public void run() {
            System.out.println("first.BusinessUseBenz run");
        }
    }
    
    • BusinessUseBWM.java
    public class BusinessUseBWM implements IBWM {
        @Override
        public void run() {
            System.out.println("first.BusinessUseBWM run");
        }
    }
    
    • AbstarctFactory.java
    public abstract class AbstarctFactory {
        public abstract IBWM createBWM();
        public abstract IBenz createBenz();
    }
    
    • HomeUseFactory.java
    public class HomeUseFactory extends AbstarctFactory {
        @Override
        public IBWM createBWM() {
            return new HomeUseBWM();
        }
        @Override
        public IBenz createBenz() {
            return new HomeUseBenz();
        }
    }
    
    • BusinessFactory.java
    public class BusinessFactory extends AbstarctFactory {
        @Override
        public IBWM createBWM() {
            return new BusinessUseBWM();
        }              
        @Override
        public IBenz createBenz() {
            return new BusinessUseBenz();
        }
    }
    
    抽象工厂模式的优点
    • 1 易于交换产品系列,由于具体工厂类,在一个应用中只需要在初始化的时候出现一次,这就使得改变一个应用的具体工厂变得非常容易,它只需要改变具体工厂即可使用不同的产品配置
    • 2 它让具体的创建实例的过程与客户端分离,客户端是通过它们的抽象接口操作实例,产品的具体类名也被具体工厂的实现分离,不会出现在客户代码中
    抽象工厂模式的缺点

    如果想要增加一个不同的车型,则需要增加三个类以及修改三个工厂,比较繁琐!

    使用简单工厂改进抽象工厂

    UML类图
    简单工厂改进抽象工厂UML.png
    代码
    • AllFactory.java
    public class AllFactory {
        private static final String type = "home";
        //private static final String type = "business";
        public static IBenz createBenz(){
            IBenz benz = null;
            switch (type){
                case "home":
                    benz = new HomeUseBenz();
                    break;
                case "business":
                    benz = new BusinessUseBenz();
                    break;
            }
            return benz;
        }
        public static IBWM createBWM(){
            IBWM bwm = null;
            switch (type){
                case "home":
                    bwm = new HomeUseBWM();
                    break;
                case "business":
                    bwm = new BusinessUseBWM();
                    break;
            }
            return bwm;
        }
    }
    
    
    使用反射改进抽象工厂方法
    public class AllFactory {
        private final static String type="homeUse";
        //private final static String type="businessUse";
    
        public static IBenz createBenz() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
            String className = type.substring(0, 1).toUpperCase() + type.substring(1) + "Benz";
            //获取包路径
            String packageName = new Object() {
                public String getPackageName() {
                    return this.getClass().getPackage().getName();
                }
            }.getPackageName();
            Class<? extends IBenz> clazz = (Class<? extends IBenz>) Class.forName(packageName+"."+className);
            return clazz.newInstance();
        }
        public static IBWM createBWM() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
            String className = type.substring(0, 1).toUpperCase() + type.substring(1) + "BWM";
            //获取包路径
            String packageName = new Object() {
                public String getPackageName() {
                    return this.getClass().getPackage().getName();
                }
            }.getPackageName();
            Class<? extends IBWM> clazz = (Class<? extends IBWM>) Class.forName(packageName +"."+className);
            return clazz.newInstance();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:设计模式之抽象工厂模式

          本文链接:https://www.haomeiwen.com/subject/jcvoiftx.html