Design Patterns ---- adapter patterns
Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Solved problems:
That is, the Adapter mode makes those classes that cannot work together due to interface incompatibility work together.
Class diagram:
Role in the Mode
Target Interface (Target): The interface the customer expects. The target can be a specific or abstract class or interface.
Adaptee: the class to be adapted or the adaptor class.
Adapter: encapsulate an object to be adapted and convert the original interface to the target interface.
Note: In the GoF design mode, there are two types of adapter modes: Class adapter mode and Object Adapter mode. Because the class adapter mode matches one interface with another through multi-inheritance, and C #, java and other languages do not support multi-inheritance, the Object Adapter is described here.
Advantages:
The client can call the same interface through the adapter, which is transparent to the client. This is simpler, more direct, and more compact. Existing classes are reused to solve the inconsistency between existing classes and reuse environments. Decouple the target class and adaptation class, and introduce an adapter class to reuse the existing adapter class without modifying the original code. An Object Adapter can adapt multiple adaptation class to the same target. That is, the same adapter can adapt both the adapter class and its subclass to the target interface.
Disadvantage: for object adapters, the implementation of adapter replacement is complicated.
Applicable scenarios: The system needs to use existing classes, and the interfaces of these classes do not conform to the system interfaces. You want to create a reusable class for some classes that are not closely related to each other, including some classes that may be introduced in the future. When the two classes do the same or similar things but have different interfaces. The class developed by the old system has implemented some functions, but the client can only access it in the form of another interface, but we do not want to manually change the original class. When using third-party components, the definition of component interfaces is different from that defined by the user. You do not want to modify your own interfaces, but use the functions of third-party component interfaces.