Adapter Design Pattern

Adapter Design Pattern

An adapter is, like we all know, a bridge that facilitates communication between two incompatible interfaces. Adapter pattern is no different.

Consider you have a phone (Client) that only has a typeC port and you wish to use your favorite wired headphones (External Service) with it. But you just can't plug in your old headphones straightaway. You need to use a TypeC to headphone jack adapter (Adapter). Similarly, at times in software engineering, we encounter classes that can't communicate. But we need them to communicate as per our logic. A common example would be an application that uses protobuf for internal communication but also uses an external API that returns data only in the json format. Here we need a json-to-protobuf adapter.

Adapter pattern offers us certain advantages in comparison to hardcoding this conversion logic in the application classes:

  1. The top advantage is the fact that two previously incompatible classes can now communicate with each other without the need to modify either of them.
  2. The phone (Client) can develop independently as long as it accepts TypeC connections. Thus there is no coupling between the client and the adapter.
  3. The headphones (External Service) can develop in dependently as long as they feature a headphone connector. The headphones need not have any idea about the adapter.
  4. We can plug in different types of adapters to the phone (client) without the need to make any changes to the client code. This supports the open closed principal as the code can be extended easily.

Here is what the implementation looks like :

No alt text provided for this image

In the implementation we make an adapter class and make it extend the TypeCDevice (Service). So Adapter is now a TypeCDevice. Then we put a Headphones (Client) variable in our class. This way we communicate the instructions to the headphones. Now we simply give our new adapter to the typeCPort. And Voila! Our favorite wired headphones now work with our brand new phone.

Have you used this pattern in your work before? Comment down below. Hope you learnt something new today. I write such posts regularly. Follow Prateek Mishra and learn something new every week.

To write this article I took help from the following sources:

  1. shosse, K., 2022. Adapter. [online] Refactoring.guru. Available at: <https://refactoring.guru/design-patterns/adapter> [Accessed 19 August 2022].
  2. tutorialspoint.com. 2022. Design Patterns - Adapter Pattern. [online] Available at: <https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm> [Accessed 19 August 2022].

要查看或添加评论,请登录

Prateek Mishra的更多文章

  • Performance Testing: An Overview

    Performance Testing: An Overview

    Performance testing is a crucial aspect of software development that ensures the quality and reliability of your…

    2 条评论
  • Builder Design Pattern

    Builder Design Pattern

    The builder pattern is a popular design pattern that is used to separate the construction of an object from its…

    5 条评论
  • Memento Design Pattern

    Memento Design Pattern

    Memento design pattern is used to save the state of an object at any point of time and then later go back to the older…

  • Iterator Design Pattern

    Iterator Design Pattern

    Iterator design pattern is one of the most used design patterns. In this article we will discuss the what, why and how…

  • Command Design Pattern

    Command Design Pattern

    Command Pattern is one of the Behavioral Design Patterns which focuses on how objects and classes should communicate…

  • Chain of Responsibility Pattern

    Chain of Responsibility Pattern

    Consider that you need an allowance for your team and for that you need to take budget approval from your manager…

    1 条评论
  • Flyweight Design Pattern

    Flyweight Design Pattern

    Flyweight Pattern is a Structural Design Pattern which can be used to save memory by sharing fields between objects…

    1 条评论
  • Decorator Design Pattern

    Decorator Design Pattern

    Decorator / Wrapper is one of the most heavily used structural design pattern. You might have also come across it many…

    2 条评论
  • Composite Design Pattern

    Composite Design Pattern

    Corporate : Michael, we are having an audit. We need your branch's revenue data by the end of the day.

  • Bridge Design Pattern

    Bridge Design Pattern

    Bridge pattern is one of the Structural Design Patterns. Consider the problem given in the image below where there is…

社区洞察

其他会员也浏览了