門面模式是對系統複雜的關係處理作了一個封裝,對外提供一個簡單的接口,成員介紹:html
場景:假設積分兌換物品流程,一共有三部分別依賴三個子系統
1.積分校驗系統,查看是否有資格。
2.積分支付系統,兌換禮物,扣減積分等。
3.物流系統,兌換禮物後,進行配送流程。
/** * 禮物實體類 */ 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; } } /** * 支付子系統 */ public class PointsPaymentService { public boolean pay(PointsGift pointsGift) { //扣減積分 System.out.println("支付:" + pointsGift.getName() + " 積分紅功!"); return true; } } /** * 積分校驗子系統 */ public class QualifyService { public boolean isAvailable (PointsGift pointsGift) { System.out.println("校驗" + pointsGift.getName() + "積分資格經過,庫存經過"); return true; } } /** * 物流子系統 */ public class ShippingService { public String shipGift (PointsGift pointsGift) { //物流系統的對接邏輯 System.out.println(pointsGift.getName() + "進入物流系統"); return "666"; } } /** * 扣減積分門面類 */ public class GiftExchangeService { /** * 模擬注入 */ private QualifyService qualifyService = new QualifyService(); private PointsPaymentService pointsPaymentService = new PointsPaymentService(); private ShippingService shippingService = new ShippingService(); //模擬注入,一開始就已經有了三個依賴的子系統 // public void setQualifyService(QualifyService qualifyService) { // this.qualifyService = qualifyService; // } // // public void setPointsPaymentService(PointsPaymentService pointsPaymentService) { // this.pointsPaymentService = pointsPaymentService; // } // // public void setShippingService(ShippingService shippingService) { // this.shippingService = shippingService; // } public void giftExchange (PointsGift pointsGift) { if (qualifyService.isAvailable(pointsGift)) { //資格校驗經過 if (pointsPaymentService.pay(pointsGift)) { //若是支付積分紅功 String shippingOrderNo = shippingService.shipGift(pointsGift); System.out.println("物流訂單號:" + shippingOrderNo); } } } }
/** * 客戶端與測試類 */ public class Test { public static void main(String[] args) { PointsGift pointsGift = new PointsGift("連衣裙"); GiftExchangeService giftExchangeService = new GiftExchangeService(); // giftExchangeService.setQualifyService(new QualifyService()); // giftExchangeService.setPointsPaymentService(new PointsPaymentService()); // giftExchangeService.setShippingService(new ShippingService()); giftExchangeService.giftExchange(pointsGift); } }
校驗連衣裙積分資格經過,庫存經過 支付:連衣裙 積分紅功! 連衣裙進入物流系統 物流訂單號:666
外觀模式和中介者模式java
外觀模式和單例模式git
外觀模式和抽象工廠模式github
門面模式
: https://github.com/sigmako/design-pattern/tree/master/facade設計模式