外观模式

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

2.类型:结构型

3.适用场景:子系统越来越复杂,增加外观模式提供简单调用接口;
  构建多层系统结构,利用外观对象作为每层的入口,简化层间调用。

4.优点:简化了调用过程,无需了解深入子系统,防止带来风险;
      减少系统依赖、松散耦合;更好的划分访问层次;
      符合迪米特法则,即最少知道原则。

5.缺点:增加子系统、扩展子系统行为容易引入风险;不符合开闭原则。

6.相关设计模式:中介者模式、单例模式、抽象工厂模式。

7.实例目录package

8.实例UML类图

9.代码

 1 package com.geely.design.pattern.structural.facade;
 2 
 3 public class PointsGift {
 4     private String name;
 5 
 6     public PointsGift(String name) {
 7         this.name = name;
 8     }
 9 
10     public String getName() {
11         return name;
12     }
13 }
1 package com.geely.design.pattern.structural.facade;
2 
3 public class QualifyService {
4     public boolean isAvailable(PointsGift pointsGift){
5         System.out.println("校验" + pointsGift.getName() + "积分资格通过,库存通过");
6         return true;
7     }
8 }
1 package com.geely.design.pattern.structural.facade;
2 
3 public class PointsPaymentService {
4     public boolean pay(PointsGift pointsGift){
5         //扣减积分
6         System.out.println("支付"+pointsGift.getName()+" 积分成功");
7         return true;
8     }
9 }
 1 package com.geely.design.pattern.structural.facade;
 2 
 3 public class ShippingService {
 4     public String shipGift(PointsGift pointsGift){
 5         //物流系统的对接逻辑
 6         System.out.println(pointsGift.getName() + "进入物流系统");
 7         String shippingOrderNo = "666";
 8         return shippingOrderNo;
 9     }
10 }
 1 package com.geely.design.pattern.structural.facade;
 2 
 3 public class GiftExchangeService {
 4     private QualifyService qualifyService;
 5     private PointsPaymentService pointsPaymentService;
 6     private ShippingService shippingService;
 7 
 8     /*public void setQualifyService(QualifyService qualifyService) {
 9         this.qualifyService = qualifyService;
10     }
11 
12     public void setPointsPaymentService(PointsPaymentService pointsPaymentService) {
13         this.pointsPaymentService = pointsPaymentService;
14     }
15 
16     public void setShippingService(ShippingService shippingService) {
17         this.shippingService = shippingService;
18     }*/
19 
20     public void giftExchange(PointsGift pointsGift){
21         if(qualifyService.isAvailable(pointsGift)){
22             if(pointsPaymentService.pay(pointsGift)){
23                 String shippingOrderNo = shippingService.shipGift(pointsGift);
24                 System.out.println("物流系统下单成功,订单号是:"+shippingOrderNo);
25             }
26         }
27     }
28 }
 1 package com.geely.design.pattern.structural.facade;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         PointsGift pointsGift = new PointsGift("T恤");
 6         GiftExchangeService giftExchangeService = new GiftExchangeService();
 7         /*giftExchangeService.setPointsPaymentService(new PointsPaymentService());
 8         giftExchangeService.setQualifyService(new QualifyService());
 9         giftExchangeService.setShippingService(new ShippingService());*/
10 
11         giftExchangeService.giftExchange(pointsGift);
12     }
13 }

 

posted @ 2019-01-06 15:13  逢春  阅读(140)  评论(0编辑  收藏  举报