1. Definition of Adapter mode (Adpater pattern)
(1) Convert the interface of a class into another interface that the customer wants. The adapter mode makes it possible for those that cannot work together because the interface is incompatible .
(2) Structure and description of the adapter mode
①client client, call the domain interface target that you need
②target: Defines the interface that the client needs to be relevant to a particular domain
③adaptee: The existing interface, usually can meet the client's functional requirements, but the interface and the client requirements of the specific domain interface inconsistent, need to be adapted.
④adapter: Adapter to fit the adaptee into the target required by the client. Can be divided into object adapters (which are combined with adpatee) and class adapters (with Adaptee being an inheritance relationship)
(3) Thinking about adapter mode
① the Nature of the adapter mode: conversion matching , multiplexing function . It uses the transformation to call its own implementation, so that it can match its own implementation into the required interface, so that it can meet the needs of the client.
②adaptee and target relationships: the interface adaptee and the target interface targets are not associated, they can be completely different two interfaces.
2. Implementation of Adapter mode
(1) Common implementations of adapters
The ① adapter is usually a class that typically causes the adapter class to implement the target interface and then calls Adapteein the adapter's implementation.
The ② adapter is typically a target type , not a adaptee type (by combination, not inheritance)
(2) to fit multiple adaptee
Adapter at the time of adaptation, can adapt to a plurality of adaptee, that is, when implementing a new target function, it is necessary to invoke the functions of multiple modules, to adapt to the functions of multiple modules to meet the needs of the new interface.
(3) Default adaptation
The default adaptation refers to the implementation of the default for an interface means , it is not necessary to implement the interface directly, but instead of inheriting the default adaptation object, so that subclasses can selectively overwrite the implementation of the required interface , for the unwanted method, It is possible to use the default adaptation method .
(4) Two-way adapter: The adaptee is suitable for the target, you can also match the target into a adaptee.
3. object Adapters and class adapters
(1) Differences in implementation
Implementation of the ① object Adapter : Depends on the object combination . That is, adapter and adaptee are combinatorial relationships.
Implementation of the ② class adapter : Inherited, that is, adapter and adaptee are inheritance relationships , while Adpater also implements the target interface, in C + + is represented as multiple inheritance.
(2) Trade-offs between class adapters and object adapters
①: Class adapters Use object inheritance in a statically defined way, and object adapters use object composition as a way of dynamic composition.
② for class adapters, because adapter directly inherits Adaptee, the adapter cannot work with Adaptee subclasses because inheritance is a static relationship, and when the adapter inherits Adaptee , You can no longer deal with Adaptee subclasses .
③ for object adapters, one adapter and multiple adaptee, including Adaptee and all of its subclasses, are allowed to work together. Because the object adapter is a combination of objects , it doesn't matter if the object type is correct .
④ for class adapters, adapter can redefine partial behavior of adaptee, which is equivalent to partial implementations of subclasses overriding the parent class. But for object adapters, it is difficult to redefine the behavior of adaptee, which requires defining subclasses of adaptee to implement redefinition, and then having the adapter group subclasses.
⑤ for class adapters, only one object is introduced, and no additional references are required to indirectly get apdatee. For object adapters, additional references are needed to get adaptee indirectly.
(3) Recommendation: Try to use the object adapter implementation method . Of course, specific problems specific analysis, according to the need to choose the implementation, the most appropriate is the best.
4. Advantages and disadvantages of adapter mode
(1) Advantages
① better reusability: If the function has already been, but the interface is not compatible, then through the adapter mode can be used to better reuse these functions.
② Better extensibility: When implementing adapter functionality, you can invoke the features you have developed to naturally extend the functionality of your system.
③ increases the transparency of the class: we are accessing the target interface, but the specific implementation is delegated to Adaptee, which is transparent to the high-level module and it does not need to be cared for.
(2) Disadvantages
Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. If clearly see the call is a interface, in fact, the interior is adapted to a B interface to achieve.
5. Usage Scenarios for adapter mode
Just keep in mind that if you want to modify an existing interface, consider using an adapter to convert to the interface you want.
6. Related Modes
(1) Adapter mode and bridging mode
① are slightly similar in structure, but functionally completely different
The ② adapter pattern is to convert the functions of two or more interfaces to match, whereas the bridging mode is to separate the interface and the implementation part so that they can vary relatively independently.
(2) adapter mode and decoration mode
① in a sense, the adapter mode simulates the ability to implement simple decorative modes. You can add features before and after adaptation.
void Adaptermethod () { /// / Call Apdatee related method before calling Adaptee method adaptee->Method (); // do some work after calling the Adaptee method }
② is just similar, and the reason for this is that both design patterns are implemented using a combination of objects that can be used to perform some additional processing before and after the function of the combined object is transferred.
③ the biggest difference between the two modes is that the adapter needs to change the interface after adaptation, and the decoration mode does not change the interface , no matter how many decorations are an interface .
8th. Structural mode-Adapter mode