This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Welcome to today’s study. Today we are going to look at one of the less frequent but easy to understand patterns —- adapter patterns. This month, I will talk about Java design patterns in detail. Please click on the profile picture and follow my column. I will keep updating.

Series of articles:

Singletons of design patterns

The factory model of design pattern

The builder pattern of design patterns

Proxy patterns of design patterns

The visitor pattern of design patterns

. Under continuous update

Without further ado, let’s get to the point

Adapter mode

As the name suggests, this pattern is certainly designed for compatibility. Adaptation of two incompatible things, can be both friendly, work together.

The original definition of this pattern is that the interface of a class is transformed into another interface expected by the customer, and the adapter allows two incompatible classes to work together.

Note: The definition makes it clear that the key to the adapter pattern is transformation, which requires compatibility with existing interfaces.

We can also take a life example to explain more vividly. For example, the projector we used in the meeting, the open interface of the computer is not compatible with the interface on the large screen, this time we need an intermediate adapter to convert.

We programmers usually need split screens, one for code, one for documentation, and one for information. Connecting three screens together does not necessarily apply to all interfaces. My MAC, for example, is not compatible. Only the Flat MAC interface is available on the MAC. You have to buy your own adapter (which has VGA,HDMI, network cable jack), so this adapter, plays an irreplaceable role (the picture below is an adapter, also called an extender)

Break it down according to the class diagram

The general class diagram for the adapter pattern is as follows:

From the redrawing, you can see three key characters (the yellow ones)

  • The target class, the interface the user expects, the adapter class the abstract class or interface to be adapted;

  • An adapter class, which can be a class or an interface, is used as an intermediate class for a concrete adaptor class.

  • A concrete adaptor class can be an internal class or service or an external object or service.

It’s not hard to see that the adapter pattern encapsulates three important facts

  • A concrete adaptor class can have different interfaces

  • The user is actually using multiple interfaces when using the adapter class;

  • Changes have been introduced to the adapter and concrete adaptor classes.

As shown in the diagram below, the classes in the adapter pattern actually act as intermediaries to encapsulate the changes.

Let’s take a closer look at the actual code

The code shown

Scenario: Take a split screen scenario where our programmers connect to a MAC and write down the code based on the class diagram and instructions described above (be sure to read the comments)

// Create the Target interface
public interface Target {
    // This is a method that the source Adapteee class does not have
    public void Request(a); 
}


// Create source class (Adaptee)
public class Adaptee {
    
    public void SpecificRequest(a){}}// Create an Adapter class
// The Adapter inherits from the Adaptee and implements the Target interface.
public class Adapter extends Adaptee implements Target {

    // The target interface calls Request(), but the source Adaptee class has no Request() method.
    // So the adapter supplements the method name
    // but actually Request() is just the content of calling the SpecificRequest() method of the source Adaptee class
    @Override
    public void Request(a) {
    
        Important: / /
        // The adapter simply encapsulates the SpecificRequest() method into a Request() that Target can call
        // This is compatibility. This is conversion. The interface of our Macintosh is connected to the split screen using an adapter (extender)
        this.SpecificRequest(); }}Copy the code

Next, you define the specific use of the target class and invoke the required methods through the Adapter class to achieve the goal

public class AdapterPattern {

    public static void main(String[] args){
        Target mAdapter = newAdapter ();// Looks like calling request. Actually is SpecificRequest,MAdapter. Request (); }}Copy the code

This is the adapter pattern. Transform the interface of one class into another interface that the client expects, so that two classes that otherwise wouldn’t work together because of interface mismatches can work together.

conclusion

Personally, I think you can use this mode when you encounter the following situations:

  • The original interface cannot be modified

  • For different data formats

  • When you need to upgrade the old interface (for example, app upgrade requires a review period, but the background code is updated all at once)

I think the adaptor pattern has many advantages, and then to get it means to sacrifice some. All patterns are the same

Advantages:

  • Decouple: Decouple the target class from the adaptor class by introducing an adapter class that reuses the existing adaptor class without modifying the original code

  • Strong scalability: in the implementation of the adapter function, you can call their own development of the function, any extension

  • Conforms to the open-close principle: the same adapter can adapt both the adaptor class and its subclasses to the target interface; Different adapters can be implemented for different target interfaces without modifying the adaptive classes

Disadvantages: I think there are the following:

1. If too many adapters are used, it will lead to bloat and the whole system will be very messy.

2. If the target interface is modified, all adaptation interfaces need to be modified

3, only one abstract class or interface can be adapted at a time (because Java is single inheritance, multiple implementations);

overtones

OK, that’s all for today’s adaptor, thank you for reading, if you feel learned, please click like, follow.

I have included this chapter in my project, click on the project below, and follow the column. I will publish dry items every day, and I will continue to enter design patterns this month.

Come on! See you next time