AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

一文徹底搞明白外觀模式

來源: 責編: 時間:2024-05-11 09:20:22 207觀看
導讀本篇講解Java設計模式中的外觀模式,分為定義、模式應用前案例、結構、模式應用后案例、適用場景、模式可能存在的困惑和本質探討7個部分。定義外觀模式是為子系統中的一組接口提供一個一致的界面,外觀模式定義了一個高

本篇講解Java設計模式中的外觀模式,分為定義、模式應用前案例、結構、模式應用后案例、適用場景、模式可能存在的困惑和本質探討7個部分。zkI28資訊網——每日最新資訊28at.com

定義

外觀模式是為子系統中的一組接口提供一個一致的界面,外觀模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。zkI28資訊網——每日最新資訊28at.com

在新的分類方式中,外觀模式被劃分至類之間的交互類別中,其簡化的是一個類與一組類之間的交互耦合問題。zkI28資訊網——每日最新資訊28at.com

zkI28資訊網——每日最新資訊28at.com

模式應用前案例

在外觀模式中,列舉一個電商領域的案例。先來看一下未使用外觀模式前的代碼實現。zkI28資訊網——每日最新資訊28at.com

電商領域通常包括庫存子系統、支付子系統和物流子系統,代碼如下。zkI28資訊網——每日最新資訊28at.com

public class InventorySystem {//庫存子系統    public void updateInventory(String product, int quantity) {        System.out.println("Updating inventory for " + product + ": " + quantity);    }}public class PaymentSystem {//支付子系統    public void processPayment(double amount) {        System.out.println("Processing payment: $" + amount);    }}public class ShippingSystem {//物流子系統    public void shipOrder(String address) {        System.out.println("Shipping order to address: " + address);    }}

調用方代碼如下。zkI28資訊網——每日最新資訊28at.com

public class Client {//調用方代碼    public static void main(String[] args) {        InventorySystem inventory = new InventorySystem();        PaymentSystem payment = new PaymentSystem();        ShippingSystem shipping = new ShippingSystem();        inventory.updateInventory("Computer", 1);        payment.processPayment(1500);        shipping.shipOrder("123 Main Street");    }}

在上述代碼中,不難發現,調用方與各個子系統直接耦合,這樣主要帶來兩個問題。zkI28資訊網——每日最新資訊28at.com

一個問題是調用方需要知曉每一個子系統的細節。在某些情況下,這些子系統之間的關系也需要知曉。zkI28資訊網——每日最新資訊28at.com

另一個問題是如果子系統代碼發生變更,調用方代碼也需要受到關聯影響。zkI28資訊網——每日最新資訊28at.com

結構

zkI28資訊網——每日最新資訊28at.com

外觀模式的示例代碼如下。zkI28資訊網——每日最新資訊28at.com

public class SubSystemOne {    public void MethodOne() {        System.out.println("Called SubSystemComponentOne's methodOne()");    }}public class SubSystemTwo {    public void MethodTwo() {        System.out.println("Called SubSystemComponentTwo's MethodTwo()");    }}public class SubSystemThree {    public void MethodThree() {        System.out.println("Called SubSystemComponentThree's methodThree()");    }}public class SubSystemFour {    public void MethodFour() {        System.out.println("Called SubSystemComponentFour's MethodFour()");    }}public class Facade {    private SubSystemOne componentOne;    private SubSystemTwo componentTwo;    private SubSystemThree componentThree;    private SubSystemFour componentFour;    public Facade() {        componentOne = new SubSystemOne();        componentTwo = new SubSystemTwo();        componentThree = new SubSystemThree();        componentFour = new SubSystemFour();    }    public void MethodA() {        componentOne.MethodOne();        componentTwo.MethodTwo();        componentThree.MethodThree();    }    public void MethodB() {        componentTwo.MethodTwo();        componentThree.MethodThree();        componentFour.MethodFour();    }}public class Client {    public static void main(String[] args) {        Facade facade = new Facade();        // 客戶端只需要調用外觀類提供的接口        facade.MethodA();        facade.MethodB();    }}

模式應用后案例

上述電商領域的案例,在應用外觀模式之后的代碼實現如下。zkI28資訊網——每日最新資訊28at.com

庫存子系統、支付子系統和物流子系統的代碼不變。zkI28資訊網——每日最新資訊28at.com

public class InventorySystem {//庫存子系統    public void updateInventory(String product, int quantity) {        System.out.println("Updating inventory for " + product + ": " + quantity);    }}public class PaymentSystem {//支付子系統    public void processPayment(double amount) {        System.out.println("Processing payment: $" + amount);    }}public class ShippingSystem {//物流子系統    public void shipOrder(String address) {        System.out.println("Shipping order to address: " + address);    }}

按照外觀模式,增加了一個外觀類。zkI28資訊網——每日最新資訊28at.com

public class OrderFacade {//訂單外觀類    private final InventorySystem inventory;    private final PaymentSystem payment;    private final ShippingSystem shipping;    public OrderFacade() {        this.inventory = new InventorySystem();        this.payment= new PaymentSystem();        this.shipping= new ShippingSystem();    }    //提供一個簡化方法來處理整個訂單流程    public void placeOrder(String product, int quantity,double amount,String address){        this.inventory.updateInventory(product,quantity);        this.payment.processPayment(amount);        this.shipping.shipOrder(address);    }}

最后,調用方代碼修改如下。zkI28資訊網——每日最新資訊28at.com

public class Client {    public static void main(String[] args) {        //使用外觀模式進行下單操作        OrderFacade facade= new OrderFacade();        facade.placeOrder("Computer", 1, 1500.00,"123 Main Street");    }}

可以看到,代碼的復雜性已經挪到外觀類中實現,調用方代碼變得非常簡潔清晰。zkI28資訊網——每日最新資訊28at.com

適用場景

外觀模式適用于以下場景:zkI28資訊網——每日最新資訊28at.com

1、多個子系統或接口需要通過一定的交互共同為調用方服務,如果希望子系統后續可以相對調用方獨立進行演進,可以考慮外觀模式zkI28資訊網——每日最新資訊28at.com

2、需求實現新功能時,需要依賴企業中的遺留系統的功能。由于遺留系統通常后續會安排下線。此時就不建議將遺留系統的接口直接對調用方暴露,而是在一個外觀類中封裝新增加的功能和遺留系統功能zkI28資訊網——每日最新資訊28at.com

模式可能存在的困惑

困惑1:外觀模式定義中提到的“界面”,具體是什么含義?zkI28資訊網——每日最新資訊28at.com

在外觀模式中,多個子系統屬于一個大的系統。界面可以理解為這個大系統對外暴露的契約接口。調用方只能通過界面來與系統進行交互。zkI28資訊網——每日最新資訊28at.com

本質

對于一個系統來講,對外暴露清晰簡潔的接口是非常有必要的。這不僅可以節省與調用方的溝通成本,也可以與調用方相對解耦,以便后續獨立進行演進。zkI28資訊網——每日最新資訊28at.com

在系統建設初期,和調用方會制定契約接口。但是隨著系統功能越來越多,經常會發現調用方需要依賴的接口越來越多,此時就可以將相互有關系的接口,再通過外觀類這一層進行再封裝,始終保持對外的簡潔性。zkI28資訊網——每日最新資訊28at.com

此外,在外觀模式下,外觀類通常并不新增功能,僅僅是封裝已有多個子系統的交互關系。zkI28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-87982-0.html一文徹底搞明白外觀模式

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 優化代碼性能:C#中輕松測量執行時間

下一篇: 你真的知道 NPM、Yarn 與 PNPM 之間的區別嗎?

標簽:
  • 熱門焦點
Top