I. Overview
The adapter mode (sometimes called packaging style or packaging) adapts the interface of a class to what you expect. An adaptation allows a class that cannot work together because the interface is not compatible, by encapsulating its own interface in an existing class.
Ii. Basic Components
Target: The interface the customer expects (can be a specific or abstract class)
Adaptee: the class to be adapted (you need to call but cannot call it directly)
Adapter: Set the identifier (inheriting the target expected by the user, and then calling the class adaptee that really needs to be called Based on the interface that the user expects)
# Include <iostream> using namespace STD; Class target {public: Virtual void request () {cout <"common request" <Endl ;}}; class adaptee {public: void specificrequest () {cout <"special request" <Endl ;}}; class adapter: public target // remember that there are a total of inheritance {PRIVATE: adaptee * adaptee; // = new adaptee (); Public: void request () {adaptee-> specificrequest () ;}; int main () {target * target = new adapter (); target-> request ();}
Iii. Adapter example
In the NBA, coaches arrange tactics in English and Yao Ming cannot understand them. He needs to translate them into explanations. It is to convert the coach interface into a Chinese language that Yao Ming can understand.
# Include <iostream> using namespace STD; // basketball player class player {protected: string name; public: Player (string name) {This-> name = Name ;} virtual void attack () = 0; virtual void defense () = 0 ;}; // forward class forwards: Public player {public: forwards (string name): Player (name) {} void attack () {cout <"Forward" <name <"attack" <Endl;} void defense () {cout <"Striker" <name <"Defender" <Endl ;};// center class center: Public player {public: Center (string name ): player (name) {} void attack () {cout <"center" <name <"attack" <Endl;} void defense () {cout <"center" <name <"defense" <Endl ;};// guard class guards: Public player {public: Guards (string name ): player (name) {} void attack () {cout <"Defender" <name <"attack" <Endl;} void defense () {cout <"Defender" <name <"Defender" <Endl ;}; // Foreign center class foreigncenter {/PRIVATE: public: string name; string getname () {return name;} void setname (string value) {This-> name = value;} void china_attack () {cout <"Foreign Center" <name <"attack" <Endl;} void chian_defence () {cout <"Foreign Center" <name <"defense" <Endl ;};// translator class Translator: Public player {PRIVATE: foreigncenter * wjzf; // = new foreigncenter (); Public: Translator (string name): Player (name) {wjzf = new foreigncenter (); // The object wjzf-> name = Name;} void attack () {wjzf-> china_attack ();} void defense () must be applied () {wjzf-> chian_defence () ;}}; int main () {player * B = new forwards ("Battier"); B-> attack (); player * m = new guards ("mcgredy"); m-> attack (); // player ym = new center ("Yao Ming "); player * ym = new translator ("Yao Ming"); ym-> attack (); ym-> defense (); Return 0 ;}