1. Simple Description
Adapter Mode
Converts an interface of a class to another interface that the customer wants. The adapter mode causes
Interfaces that are not compatible and cannot work together can work together.
Character: customer, interface A, and interface B
Customer: Xiao A, I cannot use this interface. You can't use this old bone to adapt to you, can you,
Well, you should learn from B. His interface is pretty good ~
Interface A: Boss, what do you mean? Okay, I accidentally got a baby during my recent practical work,
This baby can be deformed. Let's look at the interfaces like my programming little B to serve you ~
Interface B :....
VOICEOVER: Xiao A quietly went to the cave half-mountain cliff, looked at the four unattended, and then took out the adapter baby
Talking to myself...
A: Mirror sunglasses. It's wrong .. It should be: the omnipotent Adapter. Hurry up and make me small.
B's interface changes !!!
Only the sky is shining, and an adapter changes out of another small A, but what is different is that the small A interface looks like
It is like B, but the interface is changed to B, but the function of A is still its own.
Mr. A saw the success, and then happily went back to the customer.
Small A: customer, haha, look at me!
Customer: Wow, little A, you are too good. Now let me use you!
After the time of the trigger...
Customer: Ah, good, good, small A, I'm very satisfied !!! It's as comfortable as B. Haha!
A: Thank you, Hamada ~
Small B: silence ...)
2. code example
// [Small B] class ccassb {public: ccassb (); Virtual ~ Cclassb (); Virtual void show (); // Small B interface}; cclassb: cclassb () {} cclassb ::~ Cclassb () {} void cclassb: Show () {cout <Endl <"I Am a Small B. I did this, and I speak for myself! "<Endl;} // [small A] class cclassa {public: cclassa (); Virtual ~ Cclassa (); int func (int A); // small A interface}; cclassa: cclassa () {} cclassa ::~ Cclassa () {} int cclassa: func (int A) {cout <Endl <"I'm a small clerk, "<" my value is "<A <". I am responsible for this instant noodle task! "<Endl; return 0;} // [another small A], which is equivalent to the adapter // small A. the artifacts of small a change from Small B to a small Aclass canothera: public cclassb {public: canothera (INT nval, cclassa * ptra); Virtual ~ Canothera (); Virtual void show (); // protected: cclassa * m_ptra; // an instance of small a int m_nvalue;}; canothera: canothera (INT nval, cclassa * ptra): m_nvalue (nval), m_ptra (ptra) {} canothera :: ~ Canothera () {If (null! = M_ptra) delete m_ptra; m_ptra = NULL;} void canothera: Show () {If (null = m_ptra) return; m_ptra-> func (m_nvalue ); // The original interface of small}
Client
Int _ tmain (INT argc, _ tchar * argv []) {// Small B cclassb * pb = new cclassb (); // Small B interface Pb-> show (); cout <Endl; Delete Pb; Pb = NULL; // another small A was launched. canothera is a packaged small a PB = new canothera (10, new cclassa ()); pb-> show (); cout <Endl; return 0 ;}
3. Execution result
The adapter turns small A into another small a similar to Small B and provides it to the customer.
In this way, small a interfaces can be used by customers.