美文网首页
门面模式

门面模式

作者: juconcurrent | 来源:发表于2018-10-26 23:31 被阅读10次

概念

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

示例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;
    }
}

相关文章

  • 门面模式

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

  • 门面模式

    门面模式 定义 门面模式也叫做外观模式,是一种比较常用的封装模式。要求一个子系统的外部与其内部的通信必须通过一个统...

  • 门面模式

    子系统(细节)角色 门面角色 场景使用

  • 门面模式

    门面模式:个人理解,就是在客户端和实现类之间松耦合的一种方式。如果客户端需要操作很多的逻辑,可以将逻辑封装到一个类...

  • 门面模式

    1.定义# 要求一个子系统(具有很多类的一个系统)的外部与其内部的通信必须通过一个统一的对象进行。门面模式提供一个...

  • 门面模式

    门面模式,是指提供一个统一的接口去访问多个子系统的多个不同的接口,它为子系统中的一组接口提供一个统一的高层接口。使...

  • 门面模式

    门面模式的定义 门面模式(Facade Pattern)也叫做外观模式。定义:要求一个子系统的外部与其内部的通信必...

  • 门面模式

  • 门面模式

    门面模式,也叫外观模式,英文全称是 Facade Design Pattern。 翻译成中文就是:门面模式为子系统...

  • 门面模式

    1.概述 门面模式是将复杂系统隐藏,内部子系统以接口的方式提供给外部调用的一种方式。 2.UML结构图 以医院为例...

网友评论

      本文标题:门面模式

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