java设计模式——外观模式(门面模式)

一. 定义与类型

定义:门面模式,提供一个统一的接口,用来访问子系统中的一群接口,门面模式定义了一个高层接口,让子系统更容易使用

类型:结构性

二. 使用场景

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

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

三. 优缺点

优点:简化了调用过程,无需了解深入子系统,防止带来风险。

          减少系统依赖,松散耦合

          更好的划分访问层次

          符合迪米特法则,即最少知道原则

缺点:增加子系统,扩展子系统行为容易引入风险

   不符合开闭原则

四. 相关设计模式

外观模式和中介者模式

外观模式和单例模式

外观模式和抽象工厂模式

五. Coding

假设一个场景,在商城中用积分退换商品时,有几个步骤:

(1) 校验:判断当前积分是否能够兑换商品

(2) 支付:使用积分支付兑换商品

(3)物流:将兑换的商品,送到目的地

在实际的场景中,一般正常的步骤是,用户直接用积分兑换商品,并没有校验,支付,物流等细节。而是提供了一个统一的兑换接口,来访问这些子系统。

/**
 * @program: designModel
 * @description: 积分礼物
 * @author: YuKai Fan
 * @create: 2018-12-17 10:11
 **/
public class PointsGift {
    private String name;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
/**
 * @program: designModel
 * @description: 校验系统
 * @author: YuKai Fan
 * @create: 2018-12-17 10:12
 **/
public class QualifyService {
    public boolean isAvailable(PointsGift pointsGift) {
        System.out.println("校验" + pointsGift.getName() + " 积分资格通过,库存通过");
        return true;
    }
}
/**
 * @program: designModel
 * @description: 校验系统
 * @author: YuKai Fan
 * @create: 2018-12-17 10:12
 **/
public class QualifyService {
    public boolean isAvailable(PointsGift pointsGift) {
        System.out.println("校验" + pointsGift.getName() + " 积分资格通过,库存通过");
        return true;
    }
}
/**
 * @program: designModel
 * @description: 积分支付系统
 * @author: YuKai Fan
 * @create: 2018-12-17 10:13
 **/
public class PoingtsPaymentService {
    public boolean pay(PointsGift pointsGift) {
        //扣减积分
        System.out.println("支付" + pointsGift.getName() + " 积分成功");
        return true;
    }
}
/**
 * @program: designModel
 * @description: 物流系统
 * @author: YuKai Fan
 * @create: 2018-12-17 10:15
 **/
public class ShippingService {
    public String shipGift(PointsGift pointsGift) {
        //物流系统的对接逻辑
        System.out.println(pointsGift.getName() + "进入物流系统");
        String shippingOrderNo = "666";
        return shippingOrderNo;
    }
}

统一对外开放的接口

/**
 * @program: designModel
 * @description: 礼物兑换
 * @author: YuKai Fan
 * @create: 2018-12-17 10:17
 **/
public class GiftExchangeService {
    private QualifyService qualifyService;
    private PoingtsPaymentService poingtsPaymentService ;
    private ShippingService shippingService;

    public void giftExchange(PointsGift pointsGift) {
        if (qualifyService.isAvailable(pointsGift)) {
            //资格校验通过
            if (poingtsPaymentService.pay(pointsGift)) {
                //如果支付积分成功
                String shippingOrderNo = shippingService.shipGift(pointsGift);
                System.out.println("物流系统下班成功,订单号:" + shippingOrderNo);
            }
        }
    }

    public QualifyService getQualifyService() {
        return qualifyService;
    }

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

    public PoingtsPaymentService getPoingtsPaymentService() {
        return poingtsPaymentService;
    }

    public void setPoingtsPaymentService(PoingtsPaymentService poingtsPaymentService) {
        this.poingtsPaymentService = poingtsPaymentService;
    }

    public ShippingService getShippingService() {
        return shippingService;
    }

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

客户端:

/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2018-12-17 10:21
 **/
public class Test {
    public static void main(String[] args) {
        PointsGift pointsGift = new PointsGift("T shirt");
        GiftExchangeService giftExchangeService = new GiftExchangeService();

        giftExchangeService.setQualifyService(new QualifyService());
        giftExchangeService.setPoingtsPaymentService(new PoingtsPaymentService());
        giftExchangeService.setShippingService(new ShippingService());

        giftExchangeService.giftExchange(pointsGift);
    }
}

UML类图:

java设计模式——外观模式(门面模式)

看上面的UML类图,应用层与子系统依旧存在着联系,这是因为没有集成spring的依赖注入的原因,而是直接在应用层中创建了子系统的对象注入到外观对象中。

进一步完善后的代码:

/**
 * @program: designModel
 * @description: 礼物兑换
 * @author: YuKai Fan
 * @create: 2018-12-17 10:17
 **/
public class GiftExchangeService {
    private QualifyService qualifyService = new QualifyService();
    private PoingtsPaymentService poingtsPaymentService = new PoingtsPaymentService();
    private ShippingService shippingService = new ShippingService();

    public void giftExchange(PointsGift pointsGift) {
        if (qualifyService.isAvailable(pointsGift)) {
            //资格校验通过
            if (poingtsPaymentService.pay(pointsGift)) {
                //如果支付积分成功
                String shippingOrderNo = shippingService.shipGift(pointsGift);
                System.out.println("物流系统下班成功,订单号:" + shippingOrderNo);
            }
        }
    }

    public QualifyService getQualifyService() {
        return qualifyService;
    }

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

    public PoingtsPaymentService getPoingtsPaymentService() {
        return poingtsPaymentService;
    }

    public void setPoingtsPaymentService(PoingtsPaymentService poingtsPaymentService) {
        this.poingtsPaymentService = poingtsPaymentService;
    }

    public ShippingService getShippingService() {
        return shippingService;
    }

    public void setShippingService(ShippingService shippingService) {
        this.shippingService = shippingService;
    }
}
/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2018-12-17 10:21
 **/
public class Test {
    public static void main(String[] args) {
        PointsGift pointsGift = new PointsGift("T shirt");
        GiftExchangeService giftExchangeService = new GiftExchangeService();

//        giftExchangeService.setQualifyService(new QualifyService());
//        giftExchangeService.setPoingtsPaymentService(new PoingtsPaymentService());
//        giftExchangeService.setShippingService(new ShippingService());

        giftExchangeService.giftExchange(pointsGift);
    }
}

UML类图:

java设计模式——外观模式(门面模式)

上面的新类图,才是真正的外观模式的结构类图,支持了迪米特法则。

在开发过程中,要注意应用层到底有没有与子系统发生关系,要是又有了一个新的子系统,那就不符合开闭原则了。如果这个系统以后不需要扩展,或者扩展的子系统非常有限,那就用实体外观类就可以了,可以减少复杂度。但是如果需要经常新加子系统,那就需要使用抽象外观类。

六. 源码分析

springjdbc中的closeConnection(),closeStatement(),closeResultSet()等方法

mybatis中的Configuration配置类,使用的也是外观模式思想

tomcat源码中RequestFacade是一个request的一个外观类,实现了HttpServletRequest接口,

  还有一个Request类也实现了HttpServletRequest接口,在request里声明了RequestFacade对象,而且具体的操作都是用的这个对象

  tomcat源码中大量的使用了外观类