This is the 21st day of my participation in Gwen Challenge
This series of articles will update the basics of design patterns, and you are welcome to follow.
This article begins with an introduction to the adapter pattern. The adapter pattern is a structural pattern.
Definition of the adapter pattern
The adapter pattern translates the interface of a class into another interface representation that the client expects, for compatibility purposes, so that two classes that are originally mismatched by interfaces can work together, known as wrappers.
The most typical example is a lot of feature phones, each model has its own electrical appliances, one day its own charger is broken, and the market does not have this type of charger to buy. How to do? A universal charger will do the trick. This universal charger is the adapter.
They fall into three categories: class adapters, object adapters, and interface adapters.
Applicable scenarios:
- The system needs to use some existing classes, but the interface of these classes does not meet the needs of the system, and there is not even the source code of these classes;
- Create a reusable class that works with classes that have little to do with each other, including classes that may be introduced in the future.
structure
Composition of the adapter pattern
- Target: – Defines domain-specific interfaces used by clients.
- Client: Works with objects that conform to the Target interface.
- Adaptee: Defines an existing and used interface that needs to be adapted.
- Adapte Role: The core of the adapter pattern. It converts existing interfaces that are adapted to the Adaptee role to interfaces that match the Target role. Adaptee and Target interfaces are adapted.
The class adapter
Adapter functionality is implemented through inheritance.
Adapter and Adaptee are inherited:
- A specific Adapter class is used to match the Target. The result is that when we try to match one class and all its subclasses, the class Adapter won’t do the job
- Enables Adapter to redefine part of Adaptee’s behavior, since Adapter is a subset of Adaptee
- Simply introducing an object does not require additional Pointers to get the adaptee indirectly
Object adapter
An adapter holds an instance of the class it wraps around. In this case, the adapter calls the physical entity of the wrapped object.
Adapter and Adaptee are delegates:
- Allows one Adapter to work with multiple Adaptees simultaneously. Adapter can also add functionality to all adaptees at once
- Redefining the behavior of Adaptee can be difficult
Interface adapter
There are also many places to shoot for an interface’s adapter, as is typical in Spring where the adapter pattern is used:
In Spring’s AOP, the proxyed class is enhanced by using Advice. Spring implements this AOP functionality using the proxy pattern (JDK dynamic proxy and CGLib bytecode generation technology proxy). The class method level section enhancement, that is, the generation of proxy class proxy class, and in front of the proxy class method, set the interceptor, through the implementation of the interceptor content enhanced the function of the proxy method, the realization of section-oriented programming.
Advice types include BeforeAdvice, AfterReturningAdvice, and ThrowSadvice. Each type of Advice has corresponding interceptors, MethodBeforeAdviceInterceptor, AfterReturningAdviceInterceptor, ThrowsAdviceInterceptor. Spring needs to wrap each Advice into a corresponding interceptor type and return it to the container, so you need to use the adapter pattern to transform Advice.
public interface MethodBeforeAdvice extends BeforeAdvice {
void before(Method method, Object[] args, Object target) throws Throwable;
}
public interface AdvisorAdapter {
boolean supportsAdvice(Advice advice);
MethodInterceptor getInterceptor(Advisor advisor);
}
Copy the code
Comparison with other models
The bridge model
The bridge pattern is similar to the object adapter, but the bridge pattern has a different starting point: the purpose of the bridge pattern is to separate the interface part from the implementation part so that they can be changed easily and independently. The object adapter pattern means changing the interface of an existing object.
Decorator mode
The decorator pattern enhances the functionality of other objects without changing their interfaces. The decorator pattern is therefore more transparent to the application than the adapter. It turns out that the Decorator pattern supports recursive composition, which is not possible using an adapter alone.
The appearance model
The adapter pattern focuses on changing the API of a single class. The purpose of a Facade is to provide a cleaner interface to an entire subsystem composed of many objects. The adapter pattern, which encapsulates a single class, is often used in situations where you need third-party apis to work together, trying to isolate your code from third-party libraries.
Both the adapter pattern and the facade pattern encapsulate an existing system. However, the intent of these two patterns is completely different. The former enables the existing system to work with the system being designed while the latter provides a more convenient access interface to the existing system. Simply put, adapter patterns are designed after the fact, whereas appearance patterns must be designed beforehand because the system depends on appearance. In short, the adapter pattern does not introduce a new interface, whereas the facade pattern defines an entirely new interface.
conclusion
Regardless of the type of adapter, the purpose is to retain the services provided by existing classes and provide interfaces to the customer to meet the customer’s expectations. That is to provide new interface services without changing the original system.
Advantages are:
- You can have any two unrelated classes run together;
- Improved class reuse, want to use existing classes, and such interface standards do not meet the needs of the existing system. The adapter pattern allows for better reuse of these functions;
- Increased class transparency so that the client only cares about the result;
- When you use an adapter, you can invoke your own developed functionality, naturally extending the functionality of your system.
Disadvantages:
- Excessive use can lead to a cluttered system and traceability difficulties.