First, UML diagram
Keywords: client requires the request () function, Adaptee provides a specificrequest () function, adapter provides a request () function to encapsulate the adaptee and specificerequest () functions.
Second, the concept
Adapter Mode: transforms the interface of a class into another interface that the customer wants. The adapter mode makes it possible for those classes that cannot work together because the interface is incompatible.
Third, the description
Role:
(1) Target: This is the interface that the client expects, and target can be either a detailed or abstract class or an interface.
(2) Adaptee: A class that needs to be adapted.
(3) Adapter: the source interface is converted to the target interface by wrapping a Adaptee object inside.
When do you use it?
(1) If you want to use an existing class, but assume that his interface, that is, its methods and your requirements are not the same, you should consider using the adapter mode.
(2) The adapter mode is used. Customer code can be unified to call the unified interface can be, this can be simpler. More direct, more compact.
(3) To use adapter mode when both sides are not too easy to change, instead of using it in a different way.
Iv. implementation of C + +
(1) Adapter.h
#ifndef adapter_h#define adapter_h#include <string> #include <iostream>//target, here is the athlete class player{ Protected:std::string Name;public:player (std::string name) {this->name=name;} virtual void Attack () =0;virtual void Defense () =0;};/ /adaptee, here is the foreign center. Its interface is different from the target interface and needs to be translated to help convert class foreigncenter{private:std::string name;public:void SetName (std::string name) { This->name=name;} std::string GetName () {return name;} void Foreignattack () {std::cout<< "Foreign Center" <<name<< "offensive" <<STD::ENDL; void Foreigndefense () {std::cout<< "Foreign Center" <<name<< "defensive" <<std::endl;}};/ /adapter, here is the translation class Translator:public player{private:foreigncenter* wjzf;public:translator (std::string name): Player (name) {wjzf=new foreigncenter;wjzf->setname (name);} ~translator () {delete wjzf;} void Attack () {wjzf->foreignattack ();} void Defense () {wjzf->foreigndefense ();}};/ /below is the normal interface and the target interface of the same 3 sub-class, must be translated//striker Class Forwards:public Player{public:forwards (std::string name):P layer (NAMe) {}void Attack () {std::cout<< "striker" <<name<< "offensive" <<STD::ENDL;} void Defense () {std::cout<< "striker" <<name<< "defensive" <<std::endl;}};/ /Center Class Center:public Player{public:center (std::string name):P Layer (name) {}void Attack () {std::cout<< "center" <<name<< "attacking" <<STD::ENDL;} void Defense () {std::cout<< "center" <<name<< "defensive" <<std::endl;}};/ /Defender Class Guards:public Player{public:guards (std::string name):P Layer (name) {}void Attack () {std::cout<< "defender" <<name<< "attacking" <<STD::ENDL;} void Defense () {std::cout<< "defender" <<name<< "defense" <<std::endl;}}; #endif
(2) Client.cpp
#include "Adapter.h" #include <iostream> #include <cstdlib> #include <string>//clientvoid main () { player* b=new forwards ("Battier"); B->attack (); player* m=new Guards ("McGrady"); M->attack ();//translation Tell Yao Ming. The coach makes you both offensive and defensive player* Ym=new Translator ("Yao Ming"); Ym->attack (); Ym->defense ();d elete b;delete m;delete ym;system (" Pause ");}
(3) Implementation
Big talk design pattern C + + implementation-17th Chapter-Adapter Mode