设计模式之抽象工厂模式

2018-09-05  本文已影响0人  正在加载更多
定义

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

角色
UML类图
抽象工厂模式UML类图
代码
public interface IBenz {
    void run();
}
public interface IBWM {
    void run();
}
public class HomeUseBenz implements IBenz {
    @Override
    public void run() {
        System.out.println("first.HomeUseBenz run");
    }
}
public class HomeUseBWM implements IBWM {
    @Override
    public void run() {
        System.out.println("first.HomeUseBWM run");
    }
}
public class BusinessUseBenz implements IBenz {
    @Override
    public void run() {
        System.out.println("first.BusinessUseBenz run");
    }
}
public class BusinessUseBWM implements IBWM {
    @Override
    public void run() {
        System.out.println("first.BusinessUseBWM run");
    }
}
public abstract class AbstarctFactory {
    public abstract IBWM createBWM();
    public abstract IBenz createBenz();
}
public class HomeUseFactory extends AbstarctFactory {
    @Override
    public IBWM createBWM() {
        return new HomeUseBWM();
    }
    @Override
    public IBenz createBenz() {
        return new HomeUseBenz();
    }
}
public class BusinessFactory extends AbstarctFactory {
    @Override
    public IBWM createBWM() {
        return new BusinessUseBWM();
    }              
    @Override
    public IBenz createBenz() {
        return new BusinessUseBenz();
    }
}
抽象工厂模式的优点
抽象工厂模式的缺点

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

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

UML类图
简单工厂改进抽象工厂UML.png
代码
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();
    }
}

上一篇下一篇

猜你喜欢

热点阅读