The adapter pattern belongs to the structural design pattern, which is the top of the structural design pattern (the most used structural design pattern).
The adapter design pattern is also not complex, and the adapter is primarily used to convert the interface of a class to another interface that the customer wishes, so that those classes that could not work together because of incompatible interfaces could work together. There are two types of adapter modes: 1. Class Adapter 2. Object adapter, object adapter more.
Example: For example, you bought a mobile phone on the internet, but the buyer sent you back a 3-connector charger, but it happens that you do not have 3 connector slots, only 2 connector slots, so you will be very straight to find you a 3 interface to switch to the two connectors. The simple analysis of this converter is our adapter adapter here. Three-phase plug is what we need to fit the adaptee, two-phase interpolation adapter-----In my opinion C + + class adapter with multiple inheritance implementation, and provide the appropriate interface.
This is your three-phase plug.
Class Threephaseoutlet
{public
:
void Dothreephaseplugin ()
{
cout<< "three-phase plug plugged in strongly!" "<<endl;
}
};
This is the two-phase plug you want.
Class Twophaseoutlet
{public
:
virtual void doplugin () = 0;
Then you will need to find a adaptor that converts the three-phase plug into a "two-phase plug"
Class Outletconvertor:public Twophaseoutlet,public threephaseoutlet
{public
:
void Doplugin ()
{
doconvertor ();
Dothreephaseplugin ();
}
void Doconvertor ()
{
cout<< "three-phase plug into the hatchback plug!" "<<endl;
}
};
Now you can insert a strong plug into the two-phase socket.
twophaseoutlet* outlet = new Outletconvertor ();
Outlet->doplugin ();
Object Adapter Mode-----Object adapter is to package the objects that need to be matched and then provide the appropriate interface.
The code for the three-phase socket and the adapter for the object adaptor is consistent with the above. Just inconsistent integration steps
Class Outletconvertor:public Twophaseoutlet
{public
:
void Doplugin ()
{
doconvertor ();
M_out.dothreephaseplugin ();
}
void Doconvertor ()
{
cout<< "three-phase plug into the hatchback plug!" "<<endl;
}
Threephaseoutlet m_out;
};
The object adapter is more flexible than the class adapter, and he can selectively match the objects he wants to fit. For example, if we change the code like this, you might understand why I say this:
Class Outletconvertor:public Twophaseoutlet
{public
:
outletconvertor (Threephaseoutlet out)
{
m_out = out;
}
void Doplugin ()
{
doconvertor ();
M_out.dothreephaseplugin ();
}
void Doconvertor ()
{
cout<< "three-phase plug into the hatchback plug!" "<<endl;
}
Threephaseoutlet m_out;
};
When we construct it, we will be able to fit the object by passing in different objects according to the appropriate matching object. The class adapter is not able to select the object, he is the entire class to fit. That is, all three-phase sockets are converted to two-phase, not to one.
use adapter mode in the following situations:
1, the system needs to use existing classes, and this kind of interface does not meet the needs of the system.
2. You want to create a reusable class that works with some classes that don't have much to do with each other, including classes 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 many existing subclasses, if you use the adapter mode of the class, it is necessary to make an adapter for each subclass, which is impractical.
The adapter model has the following notable areas for implementation:
1, the target interface can be omitted, the pattern has degraded. But this practice seems mediocre and not mediocre, it can make adaptee do not need to implement unwanted methods (can refer to the default adapter mode). The manifestation is that the parent class implements the default method, and subclasses only need to implement their own unique methods. This is somewhat like a template (Template) pattern.
2, the adapter class can be an abstract class.
3, the adapter mode with parameters. With this approach, the adapter class can return a suitable instance to the client based on the parameters.