外观模式

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

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

      类型:结构性

2、适用场景

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

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

3、优点

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

  减少系统依赖、松散耦合

  更好的划分访问层次

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

4、缺点

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

  不合符开闭原则

5、实现

/**
 * @description 商品类
 * @author: hq
 * @create: 2022-08-28 14:50
 **/
public class PointsGift {

    private String name;

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

    public String getName() {
        return name;
    }

}
/**
 * @description 资格验证系统
 * @author: hq
 * @create: 2022-08-28 14:51
 **/
public class QualifyService {

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

}
/**
 * @description 积分支付系统
 * @author: hq
 * @create: 2022-08-28 14:53
 **/
public class PointsPaymentService {

    public boolean pay(PointsGift pointsGift) {
        //扣减积分
        System.out.println("支付" + pointsGift.getName() + " 积分成功");
        return true;
    }

}
/**
 * @description 商品购买系统
 * @author: hq
 * @create: 2022-08-28 14:54
 **/
public class ShippingService {

    public String shipGift(PointsGift pointsGift) {
        // 物流系统的对接逻辑
        System.out.println(pointsGift.getName() + "进入物流系统");
        String shippingNo = "66666";
        return shippingNo;
    }

}
/**
 * @description 物流系统
 * @author: hq
 * @create: 2022-08-28 14:57
 **/
public class GiftExchangService {

    private QualifyService qualifyService = new QualifyService();

    private PointsPaymentService pointsPaymentService = new PointsPaymentService();

    private ShippingService shippingService = new ShippingService();


    public void giftExchange(PointsGift pointsGift) {
        if (qualifyService.isAvailable(pointsGift)) {
            // 资格校验通过、库存通过
            if (pointsPaymentService.pay(pointsGift)) {
                // 积分扣减成功
                String shippingNo = shippingService.shipGift(pointsGift);
                System.out.println("物流系统下单成功,订单号:" + shippingNo);
            }
        }
    }
}
/**
 * @description
 * @author: hq
 * @create: 2022-08-28 15:01
 **/
public class Test {

    public static void main(String[] args) {
        PointsGift pointsGift = new PointsGift("手机");
        GiftExchangService giftExchangService = new GiftExchangService();

        giftExchangService.giftExchange(pointsGift);
    }

}

 

posted @ 2022-08-28 14:46  放手解脱  阅读(55)  评论(0)    收藏  举报