Practical Design Pattern 08 - Adapter Pattern

The word adapter is Adapter, we often encounter XxxAdapterclasses called when developing. At this time, the adapter mode is generally used. The adapter mode is very commonly used. This article will give a brief introduction to the adapter mode.

1. Introduction of problems in real development scenarios

Assume that the current system has a log interface, but its implementation performance is relatively low. When a developer wants to change to a high-performance log framework, he encounters the problem of incompatibility between the new framework and the original interface. What should he do at this time? How to adapt the new logging framework to the original logging interface.

In fact, without considering the design pattern, you can also think of a solution, which is to use the interfaces and methods of the new framework to implement the original functions in the original interface implementation.

But because a framework is used, it is not a good idea to modify the previous implementation, which is more troublesome. At this time, the adapter mode can be considered.

2. Explanation of adapter mode

The adapter mode is a solution adopted when the two layers are incompatible. Simply put, it means adding another layer.

2.1 Core classes and class diagrams

Insert image description here
For the example in the figure Client, the object I want to use now cannot be used directly because the interface has been fixed and is also used in the code. It is impossible to modify the code, so define an Adapter object as an The middle layer provides Adaptee functions to the Client.FormerImplAdapteeFormerImpl

2.2 Basic code

  • Target.java
public interface LogFace {
    
    
    void logInfo(String info);
}
  • FormerImpl.java
public class FormerImpl implements LogFace{
    
    
    @Override
    public void logInfo(String info) {
    
    
        System.out.println(info);
    }
}
  • Adaptee.java
@Slf4j
public class Adaptee {
    
    
    public void logInfo(String info){
    
    
        log.info("高性能日志实现"+info);
    }
}
  • Adapter.java
public class Adapter implements LogFace{
    
    
    private Adaptee adaptee = new Adaptee();
    @Override
    public void logInfo(String info) {
    
    
        adaptee.logInfo(info);
    }
}

  • LogFactory.java: use the factory method to get the log gate object
public class LogFactory {
    
    
	//更换日志实现,只需要在此处返回new Adapter的对象返回即可
    private static LogFace logFace = new Adapter();
    public static LogFace getInstance(){
    
    
        return logFace;
    }
}
  • Client.java
public class Client {
    
    
    public static void main(String[] args) {
    
    
        LogFace logFace = LogFactory.getInstance();
        logFace.logInfo("打印日志");
    }
}

Note: Add a factory method to the example code to create an implementation object of the log facade interface (LogFace). There is no change in the client code compared to before the log implementation was replaced.

3. Use adapter mode to solve problems

See sample code.

4. Application examples of adapter pattern

One of the most typical applications of the adapter pattern is in SpringMVC, where HandlerAdapter plays the role of an adapter.
Insert image description here
When receiving a client request, DispatcherServlet first finds out whether there is a corresponding processor from the handler mapper (HandlerMapping) based on the requested URL, etc., and then hands it to the corresponding HandlerAdapter to execute the method of processing the request. in:

  • Target: The HandlerAdapter interface plays the role of Target in the adapter mode, and all adapters must implement this interface.
  • Adapter: The specific implementation class of HandlerAdapter is the Adapter in this mode, which actually executes the processing method of the corresponding type of request.
  • Adaptee: Adaptee is a Controller, such as classes annotated by @Controller and @RestController
  • Client: The client is the SpringMVC framework. When a request arrives, it uses the appropriate HandlerAdapter based on the type of controller. The HandlerAdapter then calls the controller's methods to handle the request and produce a response.

Note:
The idea of ​​using the mode here is different from the one mentioned above. There are many Adaptees here (generally there are many Controllers), so it is more to adapt different Controllers to perform unified processor method execution and JDBC interface. same effect.

But the essence is to add a layer to shield the differences. Therefore, interface-oriented programming itself has a bit of the adapter pattern in it.

5. Summary

5.1 Problems solved

The main solution is that in software systems, it is often necessary to put some "existing objects" into a new environment, and the interfaces required by the new environment cannot be satisfied by the existing objects.

5.2 Usage Scenarios

  1. The system needs to use an existing class, and the interface of this class does not meet the needs of the system.
  2. Want to create a class that can be reused to work with some classes that are not very related to each other, including some classes that may be introduced in the future.These source classes do not necessarily have consistent interfaces
  3. Through interface conversion, one class is inserted into another class system. (For example, tigers and birds now have a flying tiger. Without the need to add entities, add an adapter to contain a tiger object and implement the flying interface.)

5.3 Advantages and disadvantages

  • Advantages: (1) Any two unrelated classes can be run together. (2) Improved class reuse. (3) Increased the transparency of the class. (4) Good flexibility.
  • Disadvantages (1) Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, what you see is this class, but what is actually executed is another class. (2) Since JAVA inherits at most one class, it can only adapt to at most one adapter class, and the target class must be an abstract class.

Reference:
[1] Understand design patterns at once – Adapter pattern – Nuggets (juejin.cn)

[2] Adapter mode | Novice tutorial (runoob.com)

[3] Understand design patterns in seconds - Adapter Pattern - Zhihu (zhihu.com)

[4] Adapter pattern of design patterns in Spring - Zhihu (zhihu.com)

Guess you like

Origin blog.csdn.net/baidu_40120883/article/details/131880938