“This is the 30th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

This is the eighth article on design patterns, in which all the design patterns I will encounter are summarized one by one. I will write down my thoughts on each design pattern and how we can flexibly apply these design patterns in practical work. Welcome your attention. In this article we will cover the adapter pattern.

A brief introduction to the adapter pattern

The adapter pattern was created to solve the problem that two objects cannot work together because of incompatible interfaces.

Just like the adapter in life, the interface can not be used to transfer processing, can be used.

Class diagram for adapter pattern:

Each role in adapter mode (we take typeC interface mobile phone using 3.5mm headset as an example) :

  • The Target object is the object required by the system, which can be regarded as a typeC interface
  • An Adapter is equivalent to an Adapter cable
  • Adaptee Is equivalent to a 3.5mm earphone jack

Adapter mode extension, bidirectional adaptation

The concrete realization idea of adapter pattern

  • Creating the target Interface
  • Create interfaces to be adapted
  • Create an adapter that implements the target interface

A concrete implementation of the adapter pattern

Public interface Target {public void request(); } // Adaptee public class Adaptee {public void specificRequest(){// Adaptee public class Adaptee {// Adaptee public void specificRequest(){// Adaptee public class Adaptee public void specificRequest(){// Adaptee public class Adaptee Target { private Adaptee adaptee; public Adapter(Adaptee adaptee){ this.adaptee = adaptee; } public void request(){ this.adaptee.specificRequest(); }}Copy the code

Advantages and disadvantages of the adapter pattern

advantages

  • Existing classes can be reused efficiently without the need for developers to modify the original code, just through adaptor compatibility.
  • The client can transparently invoke the target interface through the adapter.
  • Decoupling the target class from the adapter class solves the problem of interface inconsistency between the target class and the adapter class.
  • In line with the open and close principle.
  • High flexibility.

disadvantages

  • Adapters need to be considered in the context of business scenarios, which can increase system complexity.
  • Reduces code readability, and using too many adapters can clutter up your code.

Application scenarios of the adapter pattern

  1. The previously developed system has classes that meet the functional requirements of the new system, but their interfaces are inconsistent with those of the new system.
  2. Use a component provided by a third party that has a different interface definition than the one you need.

Adapter Pattern Summary

The biggest advantage of the adapter mode is the reuse of code. It is ok to have a copy of the code that meets the business logic. We only need to make it meet the call of other systems through the adapter. However, because of this feature of adapters, too many adapters to satisfy different interfaces can make our code confusing and difficult to read. We use adapter mode or to be careful, small use appropriate situation, large use of the body ah.