Adapter Pattern

Adapter Pattern

Tiếp tục loạt bài Design Patterns, hôm nay tôi sẽ chia sẻ tới mọi người Adapter Pattern.

Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

😕 Problem

Imagine that you’re creating a stock market monitoring app. The app downloads the stock data from multiple sources in XML format and then displays nice-looking charts and diagrams for the user.

At some point, you decide to improve the app by integrating a smart 3rd-party analytics library. But there’s a catch: the analytics library only works with data in JSON format.

Article content

You could change the library to work with XML. However, this might break some existing code that relies on the library. And worse, you might not have access to the library’s source code in the first place, making this approach impossible.

🙂 Solution

You can create an adapter. This is a special object that converts the interface of one object so that another object can understand it.

An adapter wraps one of the objects to hide the complexity of conversion happening behind the scenes. The wrapped object isn’t even aware of the adapter. For example, you can wrap an object that operates in meters and kilometers with an adapter that converts all of the data to imperial units such as feet and miles.

Adapters can not only convert data into various formats but can also help objects with different interfaces collaborate. Here’s how it works:

  1. The adapter gets an interface, compatible with one of the existing objects.
  2. Using this interface, the existing object can safely call the adapter’s methods.
  3. Upon receiving a call, the adapter passes the request to the second object, but in a format and order that the second object expects.

Sometimes it’s even possible to create a two-way adapter that can convert the calls in both directions.

Article content

Let’s get back to our stock market app. To solve the dilemma of incompatible formats, you can create XML-to-JSON adapters for every class of the analytics library that your code works with directly. Then you adjust your code to communicate with the library only via these adapters. When an adapter receives a call, it translates the incoming XML data into a JSON structure and passes the call to the appropriate methods of a wrapped analytics object.

⚡Applicability

Use the Adapter class when you want to use some existing class, but its interface isn’t compatible with the rest of your code.

The Adapter pattern lets you create a middle-layer class that serves as a translator between your code and a legacy class, a 3rd-party class or any other class with a weird interface.

Use the pattern when you want to reuse several existing subclasses that lack some common functionality that can’t be added to the superclass.

You could extend each subclass and put the missing functionality into new child classes. However, you’ll need to duplicate the code across all of these new classes, which smells really bad.


To view or add a comment, sign in

More articles by Nguyễn Đăng Khoa

  • Chain of Responsibility Pattern

    Chain of Responsibility là một mẫu thiết kế hành vi cho phép bạn chuyển các yêu cầu dọc theo chuỗi người xử lý. Khi…

  • Proxy Pattern

    Proxy là một mẫu thiết kế cấu trúc cho phép bạn cung cấp một thay thế hoặc giữ chỗ cho một đối tượng khác. Một proxy…

  • Facade Pattern

    Facade Pattern là một structural design pattern cung cấp interface đơn giản hóa cho library, framework hoặc bất kỳ tập…

    1 Comment
  • Decorator Pattern

    Decorator Pattern là một mẫu thiết kế cấu trúc cho phép bạn đính kèm các hành vi mới vào đối tượng bằng cách đặt các…

  • Composite Pattern

    Composite là một mẫu thiết kế cấu trúc cho phép bạn kết hợp các đối tượng thành cấu trúc cây rồi làm việc với các cấu…

  • Bridge Pattern

    Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two…

  • Singleton Pattern

    Quay trở lại series design patterns, ngày hôm nay tôi sẽ chia sẻ tới các bạn Singleton Pattern. Cùng tìm hiểu nhé.

Insights from the community

Others also viewed

Explore topics