[Design mode] Translation-adapter mode required in the NBA

Source: Internet
Author: User

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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.