Juconcurrent 学而不思则罔,思而不学则殆。

门面模式


概念

为了满足一组系统接口使用变得更简单,需要提供统一的访问入口。我们可以将这一组系统接口简化为一个接口,那么也算是门面模式。

示例1 — 门面模式之前

class Camera {
    void turnOn();
    void turnOff();
    void rotate(int degrees);
}

class Light {
    void turnOn();
    void turnOff();
    void changeBulb();
}

class Sensor {
    void activate();
    void deactivate();
    void trigger();
}

class Alarm {
    void activate();
    void deactivate();
    void ring();
    void stopRing();
}

public class Client {

    private Camera camera1, camera2;
    private Light light1, light2, light3;
    private Sensor sensor;
    private Alarm alarm;

    public Client() {
        camera1 = new Camera();
        camera2 = new Camera();
        light1 = new Light();
        light2 = new Light();
        light3 = new Light();
        sensor = new Sensor();
        alarm = new Alarm();
    }

    public static void main(String[] args) {
        camera1.turnOn();
        camera2.turnOn();
        light1.turnOn();
        light2.turnOn();
        light3.turnOn();
        sensor.activate();
        alarm.activate();
    }
}

示例1 — 门面模式之后

public class SecurityFacade {

    private Camera camera1, camera2;
    private Light light1, light2, light3;
    private Sensor sensor;
    private Alarm alarm;

    public SecurityFacade() {
        camera1 = new Camera();
        camera2 = new Camera();
        light1 = new Light();
        light2 = new Light();
        light3 = new Light();
        sensor = new Sensor();
        alarm = new Alarm();
    }

    public void activate(){
        camera1.turnOn();
        camera2.turnOn();
        light1.turnOn();
        light2.turnOn();
        light3.turnOn();
        sensor.activate();
        alarm.activate();
    }

    public void deactivate() {
        camera1.turnOff();
        camera2.turnOff();
        light1.turnOff();
        light2.turnOff();
        light3.turnOff();
        sensor.deactivate();
        alarm.deactivate();
    }
}

public class Client {
    private static SecurityFacade security;

    public static void main(String[] args) {
        security = new SecurityFacade();
        security.activate();
        security.deactivate();
    }
}

示例2 — Spring单例+享元模式+门面模式实现退款支付扩展的门面

@Component public class RefundPayItemActionFacade {

    @Autowired private List<RefundPayItemAction<?, ?>> refundPayItemActions;
    @Autowired private DefaultRefundPayItemAction defaultRefundPayItemAction;
    private Map<CashType, RefundPayItemAction<?, ?>> refundPayItemActionMap = new HashMap<>();

    @PostConstruct public void init() {
        for (RefundPayItemAction<?, ?> itemAction : refundPayItemActions) {
            refundPayItemActionMap.put(itemAction.supported(), itemAction);
        }
    }

    public RefundPayItemAction<?, ?> get(CashType cashType) {
        return refundPayItemActionMap.get(cashType);
    }

    public RefundPayItemAction<?, ?> get(Long payModeId) {
        CashType cashType = CashType.valueOf(payModeId);
        RefundPayItemAction<?, ?> itemAction = null;
        if (nonNull(cashType)) {
            itemAction = refundPayItemActionMap.get(cashType);
        }
        if (isNull(itemAction)) {
            itemAction = defaultRefundPayItemAction;
        }
        return itemAction;
    }
}

上一篇 构建者模式

下一篇 装饰模式

Content