Points:
1. The adapter pattern is mainly used to "want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment", in the legacy code reuse, class library migration is very useful.
2. Adapter mode has the object adapter and class adapter in two forms of implementation structure, but the class adapter in the "Multiple inheritance" implementation, resulting in poor high coupling, so generally not recommended. Object adapters take the form of "object Combination", which is more consistent with the spirit of loose coupling.
Realize:
Adapter pattern structure diagram for Class (inheritance)
Adapter pattern diagram (combination) of objects
(Code implementation of the object adapter)
Target: Defines a specific domain-related interface used by the client
Adaptee: Existing interfaces that need to fit now
void Specificrequest () {}}
Adapter: The interface to the Adaptee and the target interface are fitted
Private Adaptee Adaptee;}
Applicability:
1. The system needs to use existing classes, which do not conform to the requirements of the system.
2. Want to create a reusable class that works with some classes that don't have too much association with one another, including some that might be introduced in the future. These source classes do not necessarily have very complex interfaces.
3. (For object adapters) in the design, you need to change the interface of multiple existing subclasses, and if you use the adapter pattern of the class, make an adapter for each subclass, which is impractical.
Effect and advantages and disadvantages:
For class adapters:
1. Match adaptee and Taget with a specific adapter class. The result is that class adapter will not work when we want to match a class and all its subclasses.
2. Allow adapter to override (redefine) adaptee part of the behavior, because adapter is a subclass of Adaptee.
For object adapters:
1. Allows a adapter to work with multiple adaptee, that is, the adaptee itself and all its subclasses (if any). Adapter can also add functionality to all adaptee at once.
2. Make override (redefine) Adaptee behavior more difficult. If you must override the Adaptee method, you have to do a adaptee subclass to override the Adaptee method, and then use the subclass as a true adaptee source.