Adapter mode
An intention
Transforms the interface of one class into another interface that the customer wants.
The adapter mode makes it possible for those classes that would otherwise be incompatible with the interface to work together.
Two motives
A generic class designed for reuse, there are always special situations that make it not possible to use or complete the appropriate extensibility functions.
See the example given in the text: New additions to the text graphics Editor in the drawing editor
TextView is to render text, does not support the way of graphic editing, the new addition of Textshape, is unable to directly use the method in TextView,
But there is also the need for a shape operation, and perhaps a new addition of features or operations that require a new class to be added to accomplish these functions.
In order to be able to use the TextView already exists in the text processing method, you need to inherit the TextView or the combination of TextView objects.
Three applicability and structure
Use a class that already exists, but the interface does not meet the requirements
Create a reusable class that works in conjunction with other unrelated classes
Object Adapter Adaptation Parent class interface
Inheritance mode-Class adapter:
Combination mode-Object adapter:
The two ways of inheriting and combining are somewhat different and need to be weighed:
Class 1 Adapters
can only ensure that the adapter match the base class Adaptee is capable of, for the Adaptee base class derived sub-class and must be fully competent.
Adapter can redefine the methods of the base class and extend its own methods
Simply introducing an object does not require additional pointers to indirectly get adaptee.
2 Object Adapter
A adapter can correspond to the adaptee itself and its subclasses
Can be extended, but if it is difficult to modify the base class itself and its subclasses
3 Deep Exploration
Adapter The amount of work to match, depending on the target and adaptee similarity
Pluggable adapters: The adapter can be applied to the system
Bidirectional matching: Compatible with the classes that are matched, both can work.
Four Code implementations
1 achieve color brightness variable lamp:
There is a lamp whose color is variable, but it now requires its luminance to be variable, and the luminance is determined by the combination of voltage and color. An adapter is required to complete this work.
Voltage Converters
Class Voltageconveter
{
Public
Voltageconveter () {cout<< "Voltageconveter constructor" <<ENDL;}
void bindinputvoltage (int voltage) {}
void adjustvoltage (int rate) {}
Private
int m_voltage;
};
Color lamp
Class Colourlight
{
Public
Colourlight () {cout<< "Colourlight constructor" <<ENDL;}
void setcolortype (int color) {}
Private
int m_colortype;
};
Class Adapter
Class Brightadapter:public Colourlight,public Voltageconveter
{
Public
Brightadapter () {cout<< "Brightadapter constructor" <<ENDL;}
void Setlightness () {}
int getlightness () {}
Private
int m_lightness;
};
2 text edit box control implementation on the phone
These include two sections: Text display and Input method. Text display is not intended for edit box service, Input method is not an edit box.
Therefore, the text display and input method can be combined to implement the text edit box. The text edit box also needs to add its own features, as well as the use of input methods.
The approximate structure diagram is as follows: