Liver-Liver Design Pattern [5] -- Adapter Pattern

Series Article Directory

Liver-Liver Design Pattern [1]--Singleton Mode Portal
Liver-Liver Design Pattern [2]--Factory Mode Portal
Liver-Liver Design Pattern [3]--Prototype Mode Portal
Liver-Liver Design Pattern [4] 】--Builder mode portal
liver-to-liver design mode [5]--adapter mode portal
liver-to-liver design mode [6]--decorator mode portal
liver-to-liver design mode [7]--proxy mode transmission Portal
Liver-Liver Design Pattern [8] -- Appearance Mode Portal
Liver-Liver Design Pattern [9] -- Flyweight Mode Portal



foreword

We know that design patterns can be divided into three categories: creational patterns, structural patterns, and behavioral patterns. In the previous article, we have finished analyzing the five creational patterns. From this section, we will analyze the structural pattern. There are seven structural patterns. First, let’s understand the adapter pattern.


1. What is the adapter pattern

Adapter Pattern (Adapter Pattern), the interface of a class into another interface that customers want. The Adapter pattern allows classes with incompatible interfaces to work together. This type of design pattern is a structural pattern that combines the functionality of two separate interfaces.

There are three roles in the adapter pattern:

  • Target: the interface desired by the client
  • Source (Adaptee): Adapted interface
  • Adapter (Adapter): adapt the Target request to the corresponding interface of Adaptee for processing

There are two main types of adapter patterns, class adapters and object adapters.

If we use daily life as an example, for example, when we travel abroad, the problem of power supply adaptation is often a problem that must be solved when traveling. The domestic voltage is 220V. If the destination is Europe, the voltage in some European countries is 110V or 120V, and The shape of the plug is not the same. At this time, we often need to bring a converter. What the converter does is actually what the adapter mode does.

Two, class adapter

The class adapter mode mainly implements the adapter function through inheritance.

Write down the code:

A suitable European socket is required

public interface EuropeSocket {
    
    
	public void power110();
}

own domestic plug

public class ChineseSocket {
    
    
    public void power220(){
    
    
    	System.out.println("220V供电");
    }
}

adapter conversion

public class SocketAdapter extends ChineseSocket implements EuropeSocket {
    
    
	@Override
	public void power110() {
    
    
		System.out.println("适配器接入欧洲插座");
		power220();
	}
}

test code

public class AdpaterTest {
    
    
    public static void main(String[] args) {
    
    
        SocketAdapter adapter = new SocketAdapter();
        System.out.println("国内插头插入适配器。");
        adapter.power110();
    }
}

3. Object Adapter

The object adapter pattern realizes the adapter function through composition.

Write down the code:

Modify the adapter code

public class SocketAdapter implements EuropeSocket {
    
    

	private ChineseSocket chineseSocket;

	public SocketAdapter(ChineseSocket chineseSocket) {
    
    
		this.chineseSocket = chineseSocket;
	}
	
	@Override
	public void power110() {
    
    
		System.out.println("适配器接入欧洲插座");
		chineseSocket.power220();
	}
}

test code

public class AdpaterTest {
    
    
    public static void main(String[] args) {
    
    
        SocketAdapter adapter = new SocketAdapter(new ChineseSocket());
        System.out.println("国内插头插入适配器。");
        adapter.power110();
    }
}

4. Application in JDK

In the JDK, the adapter mode has many application scenarios. Here are two common examples:

  • Adapter mode in the Java collection framework: Java provides a series of collection classes (such as ArrayList, LinkedList, HashMap, etc.), which implement their respective interfaces (such as List, Map, etc.). Sometimes we need to convert a collection object into another collection object of interface type, then we can use the adapter pattern. For example, a List object can be converted into a List object of a specified type through the static method checkedList() of the Collections class.
  • InputStreamReader and OutputStreamWriter classes: These two classes are adapters between character streams and byte streams in the Java IO library. InputStreamReader converts the byte stream into a character stream, and OutputStreamWriter converts the character stream into a byte stream. They enable otherwise incompatible character streams and byte streams to work together through the adapter mode.

write at the end

Class adapters and object adapters, these two adapters are more recommended to use the object adapter mode, which is more flexible than the class adapter mode, and also conforms to the principle of composition and reuse in the design principles.

Usage scenarios of adapter mode:

  • When the system is relatively old, with the development of the business, new requirements are added, but there is no way to update the system in a short time, so the method of adding an intermediate layer is adopted and the adapter mode is adopted for compatibility.

Advantages of the adapter pattern:

  • It has better reusability and flexibility.

Disadvantages of adapter pattern:

  • Excessive use of adapters will make the system more cluttered. It is obvious that the A interface is called, but it is internally adapted to the B interface. Therefore, unless necessary, it is not recommended to use adapters, but to directly refactor the system

Guess you like

Origin blog.csdn.net/u011646838/article/details/130468810