在過去的幾周里,我分享了一些流行的設計模式,例如PubSubSingleton模式。今天,我將分享本系列的另一篇文章,但請在下面評論並告訴我接下來應該介紹哪種設計模式!

適配器模式

適配器模式是一種結構設計模式,允許具有不相容介面的物件進行協作。當您想要讓現有的類別與其他類別一起工作而不修改其原始程式碼時,通常會使用它。當現有類別的介面與您需要的介面不符時,此模式特別有用。

真實案例場景

讓我們考慮一個現實生活中的例子。您的任務是將第三方視訊播放器整合到您的應用程式中。但是,視訊播放器的功能不同,並且具有與您的應用程式期望的不同的方法介面。在這種情況下,您可以使用適配器模式圍繞視訊播放器建立一個包裝類,使第三方程式碼與您現有的應用程式程式碼相容。

這是您在這種情況下使用的程式碼:

// Adapter class
class VideoPlayerAdapter {
    constructor() {
        this.externalPlayer = new ThirdPartyVideoPlayer({
            // some configuration
        });
    }

    play() {
        const video = this.externalPlayer.getVideo();
        this.externalPlayer.playVideo(video, {
            // additional parameters
        });
    }
}

// Your application code
class Application {
    constructor() {
        this.videoPlayer = new VideoPlayerAdapter();
    }

    start() {
        // Play video using your application code
        this.videoPlayer.play();
    }
}

我們來分解一下上面的程式碼:

  1. ThirdPartyVideoPlayer是您的應用程式想要使用的假設外部程式庫。但是,它的介面可能與您的應用程式不相容。

  2. VideoPlayerAdapter是適配器類別。它圍繞著ThirdPartyVideoPlayer 。此適配器的介面與您的應用程式相容。當適配器的play()方法被呼叫時,它會在內部呼叫ThirdPartyVideoPlayer上的必要方法。

  3. Application是您的應用程式程式碼。它建立VideoPlayerAdapter的實例並像常規視訊播放器一樣使用它。當它呼叫適配器上的play()方法時,適配器會將其轉換為對ThirdPartyVideoPlayer的適當呼叫。

這樣, Application類別就不需要了解ThirdPartyVideoPlayer的工作原理。如果您需要用不同的程式庫替換ThirdPartyVideoPlayer ,您只需要編寫一個新的適配器 - Application類別可以保持不變。這是適配器模式的主要優點:它將應用程式程式碼與第三方程式庫的細節分開。

適配器模式和外觀模式之間的區別

雖然適配器模式和外觀模式看起來很相似,但它們具有不同的目的並在不同的上下文中使用:

  1. 目的
- **Adapter Pattern**: The primary purpose of the Adapter Pattern is to make two incompatible interfaces compatible with each other. It allows an existing class with a different interface to be used as if it implemented a different interface.

- **Facade Pattern**: The main purpose of the Facade Pattern is to provide a simplified interface to a complex subsystem. It hides the complexities of the subsystem and provides a higher-level interface that makes the subsystem easier to use.

  1. 用法
- **Adapter Pattern**: It is used when you need to integrate a new class or library that does not match the existing class or interface in your application. The Adapter Pattern is about adapting a particular interface to an expected interface.

- **Facade Pattern**: It is used when you want to simplify interactions with a complex subsystem. The Facade provides a straightforward method for interacting with the system, hiding its complexity.

  1. 設計
- **Adapter Pattern**: Typically involves creating a new class (the Adapter) that implements the interface expected by the client and translates calls to the adapted class.

- **Facade Pattern**: Involves creating a Facade class that provides simplified methods to the client, often aggregating multiple functionalities from the subsystem.

總之,雖然這兩種模式都提供了一種使用現有程式碼的方法,但適配器模式著重於介面相容性,而外觀模式則著重於簡化與複雜系統的互動。

極佳的邀請 - 贏取 5.000 美元

所以,當你在這裡的時候,讓我邀請你參加我們即將舉行的超級活動今年八月

這次遠端活動讓您有機會透過我們的即時通訊工具應對挑戰,改變您的虛擬交互,從而展示您的技能和創造力。有了 SuperViz,您就有機會贏得 5,000 美元的獎金。

立即註冊以接收更新、提示和資源,並準備好破解!


原文出處:https://dev.to/superviz/design-pattern-5-adapter-pattern-4gif


共有 0 則留言