Adapter mode, convert different interfaces, achieve compatibility.
Specific environment
Interface compatible
-Use existing classes, but in a different way from the original, you need to convert
-Existing interfaces are compatible with third-party components
-Retains and uses the functionality of the original system class, but the client access mode changes while the original code cannot be modified
Conversion between similar functional classes
A class of questions
Incompatible interface classes, not cooperating
Solution Solutions
3 characters:
- Destination interface target: Provides the expected usage, implemented by a class or interface
- Adaptee by the appropriate ligand: the original use of the class, implemented by the class or interface
- Adapter Adapter: Encapsulates the adaptee reference, implements or inherits Target, overrides the target method, encapsulates the Adaptee method
class adapters and Object adapters
Java does not support multiple inheritance and is not currently discussed
Advantages and Disadvantages
Pros:
- reusing classes, reducing the amount of development
- extensible Legacy class functionality
- can be decoupled via the target interface, using multiple adapters
- by the adapter to the interface user transparent
- target interface and the adapter
Cons:
for object adapters, the adapter is more complex to write or modify
Because the original system is required to provide an interface (that is, the API of the adapter), and it needs to understand its applicable environment
Reference
Design Patterns Learning Notes-Adapter mode, Wang Juqiang
Understanding Design Patterns (adapters)