重学设计模式之外观模式

90 阅读2分钟

外观设计模式

1.定义

又叫门面模式 提供了一个统一的接口,用来访问子系统中的一群接口

1.1 特点

​ 外观模式定义了一个高层接口,让子系统更容易使用

1.2 类型 : 结构型

2.适用场景

​ 1、子系统越来越复杂,增加外观模式提供简单调用接口

​ 2、构建多层系统结构,利用外观对象作为每层的入口 简化层间调用

3.缺点

1.增加子系统 扩展子系统行为容易引入风险

  1. 不符合开闭原则

4.优点

​ 1.简化了调用过程 无需了解深入子系统,防止带来风险

​ 2.减少系统依赖 松散耦合

​ 3.更好的划分访问层次

​ 4.符合迪米特法则,既最少知道原则

5.外观-相关的设计模式

外观模式和中介者模式

外观模式和单例模式

外观模式和抽象工厂模式

重点 应用层 不要直接与子系统发生交互 代码如下 1、三个子系统代码

package com.zw.design.pattern.creational.structural.facade;

public class PointsPaymentService {

    public boolean pay(PointsGift pointsGift){
        //扣减积分
        System.out.println("pointsGift = 支付积分成功 " + pointsGift.getName());
        return  true;
    }
}
package com.zw.design.pattern.creational.structural.facade;

public class QualifyService {

    public boolean isAvailable(PointsGift pointsGift){
        System.out.println("pointsGift = " + pointsGift.getName()+" 积分通过 库存通过 ");
        return  true;
    }
}

package com.zw.design.pattern.creational.structural.facade;

/****
 * 物流系统
 */
public class ShippingService {
    public  String shipGift(PointsGift pointsGift){
        //物流系统对接操作
        System.out.println("pointsGift = 进入物流系统 " + pointsGift.getName());
        String shipOrderNo="8888888";
        return  shipOrderNo;
    }
}
package com.zw.design.pattern.creational.structural.facade;

public class PointsGift {
    private String name;

    public PointsGift(String name){
        this.name=name;
    }

    public String getName() {
        return name;
    }
}

2.外观设计类

package com.zw.design.pattern.creational.structural.facade;

public class GiftExchangService {

    private QualifyService qualifyService=new QualifyService();
    private PointsPaymentService pointsPaymentService=new PointsPaymentService();
    private ShippingService shippingService=new ShippingService();

    public void setPointsPaymentService(PointsPaymentService pointsPaymentService) {
        this.pointsPaymentService = pointsPaymentService;
    }

    public void setQualifyService(QualifyService qualifyService) {
        this.qualifyService = qualifyService;
    }

    public void setShippingService(ShippingService shippingService) {
        this.shippingService = shippingService;
    }

    public void giftExchang(PointsGift pointsGift){
        if (qualifyService.isAvailable(pointsGift)){
            //资格检验通过
            if (pointsPaymentService.pay(pointsGift)){
                //支付积分成功
                String orderNa = shippingService.shipGift(pointsGift);
                System.out.println("orderNa =下单成功 " + orderNa);
            }
        }
    }
}

3.测试类

package com.zw.design.pattern.creational.structural.facade;

public class Test {
    public static void main(String[] args) {
        PointsGift pointsGift=new PointsGift("鼠标");
        GiftExchangService giftExchangService=new GiftExchangService();
//        giftExchangService.setQualifyService(new QualifyService());
//        giftExchangService.setShippingService(new ShippingService());
//        giftExchangService.setPointsPaymentService(new PointsPaymentService());
        giftExchangService.giftExchang(pointsGift);
    }
}

uml 图
在这里插入图片描述

框架源码解析

在spring框架当中jdbcUtils 也就是说 jdbcUtils对connection 进行了封装 在这里插入图片描述 在mybatis 当中使用外观设计模式应用Configuration 在这里插入图片描述 在tomcat 源码大量使用该设计模式 比如 requestFacade类当中等 代码如下