09. Adapter mode–Adapter

Hits: 0

Adapter mode: The
Adapter mode converts the interface of a class into another interface that the client wants . The Adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces .

The data and behavior of the system are correct, but the interface does not match, we should consider using an adapter, the purpose is to make an original object outside the control scope to match an interface. The adapter pattern is mainly used when you want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment. Adapter mode adaptation is usually used when both parties are not easy to modify .

For example, if the company considers using a third-party component when designing a system, and the interface of this component is different from our own system interface, and we do not need to change our own interface to cater for it, then, You can consider using adapters to solve the problem of different interfaces.

There are two ways to implement the Adapt mode, one is to inherit the original interface class, the other is to combine the original interface class.
Taking the NBA players in “Dahua Design Patterns” as an example, when Yao Ming first arrived in the NBA, he needed a translator (adapter) as a bridge for him to communicate with others.

The typical structure diagram of the Adapter pattern is:

Adapter Pattern (class pattern) structure diagram

Adapter Pattern (object pattern) structure diagram

The following Adapter Pattern (class pattern) implementation code:

//Adapter.h
#ifndef _ADAPTER_H_
#define _ADAPTER_H_

// Player 
class  Player 
{ 
public :
 Player();
 virtual ~Player();
 virtual void Attack();
protected:
private:
};

// Translator adapter 
class  TranslatorAdaptee 
{ 
public :
 TranslatorAdaptee();
 ~TranslatorAdaptee();
 void SpecificAttack();
protected:
private:
};

// Foreign players, such as Yao Ming 
class  ForeignPlayer :  public Player, private TranslatorAdaptee
{
public:
 ForeignPlayer();
 ~ForeignPlayer();
 void Attack();
};

#endif //~_ADAPTER_H_


//Adapter.cpp
#include "Adapter.h"
#include <iostream>

Player::Player()
{
}

Player::~Player()
{
}

void Player::Attack()
{
 std::cout<<"Player::Attack..."<<std::endl;
}

TranslatorAdaptee::TranslatorAdaptee()
{
}

TranslatorAdaptee::~TranslatorAdaptee()
{
}

void TranslatorAdaptee::SpecificAttack()
{
 std :: cout << "TranslatorAdaptee::SpecificAttack...Foreign Player Attack" << std :: endl ;
}

ForeignPlayer::ForeignPlayer()
{
}

ForeignPlayer::~ForeignPlayer()
{
}

void ForeignPlayer::Attack()
{
 this->SpecificAttack();
}


//main.cpp
#include "Adapter.h"
#include <iostream>
using namespace std;

int  main ( int argc, char * argv[])
 {
  // normal players attack 
 Player* play = new Player;
 play->Attack();

 //Foreign player (such as Yao Ming) to attack, the translation adapter tells the foreign player 
 Player* fplay = new ForeignPlayer();
 fplay->Attack();

 return 0;
}


The following Adapter Pattern (object pattern) implementation code:

//Adapter.h
#ifndef _ADAPTER_H_
#define _ADAPTER_H_

// Player 
class  Player 
{ 
public :
 Player();
 virtual ~Player();
 virtual void Attack();
protected:
private:
};

// Translator adapter 
class  TranslatorAdaptee 
{ 
public :
 TranslatorAdaptee();
 ~TranslatorAdaptee();
 void SpecificAttack();
protected:
private:
};

// foreign players, such as Yao Ming 
class  ForeignPlayer : public Player
{
public:
 ForeignPlayer(TranslatorAdaptee* ade);
 ~ForeignPlayer();
 void Attack();
protected:
private:
 TranslatorAdaptee* _ade;
};

#endif //~_ADAPTER_H_


//Adapter.cpp
#include "Adapter.h"
#include <iostream>

Player::Player()
{
}

Player::~Player()
{
}

void Player::Attack()
{
 std::cout<<"Player::Attack..."<<std::endl;
}

TranslatorAdaptee::TranslatorAdaptee()
{
}

TranslatorAdaptee::~TranslatorAdaptee()
{
}

void TranslatorAdaptee::SpecificAttack()
{
 std :: cout << "TranslatorAdaptee::SpecificAttack...Foreign Player Attack" << std :: endl ;
}

ForeignPlayer::ForeignPlayer(TranslatorAdaptee* ade)
{
 this->_ade = ade;
}

ForeignPlayer::~ForeignPlayer()
{
}

void ForeignPlayer::Attack()
{
 _ade->SpecificAttack();
}


//main.cpp
#include "Adapter.h"
#include <iostream>
using namespace std;

int  main ( int argc, char * argv[])
 {
  // normal players attack 
 Player* play = new Player;
 play->Attack();

 // foreign players (such as Yao Ming) attack, the translation adapter tells the foreign player 
 TranslatorAdaptee* ade = new TranslatorAdaptee;
 Player* fplay = new ForeignPlayer(ade);
 fplay->Attack();

 return 0;
}

In the class mode Adapter, we obtain the effect of implementing inheritance through private inheritance of TranslatorAdaptee, and obtain the effect of interface inheritance through public inheritance of Player.
In the two modes of the Adapter mode, a very important concept is the difference and connection between interface inheritance and implementation inheritance. Interface inheritance and implementation inheritance are two important concepts in the object-oriented field.
Interface inheritance: means that through inheritance, the subclass obtains the interface of the parent class;
implementation inheritance: means that through inheritance, the child class obtains the implementation of the parent class.

Public inheritance in C++ is both interface inheritance and implementation inheritance , because after inheriting the parent class, the subclass can not only provide the interface operations in the parent class, but also obtain the interface implementation of the parent class. Of course, we can simulate separate interface inheritance and implementation inheritance through certain methods and technologies. For example, we can obtain the effect of implementation inheritance through private inheritance (after private inheritance, the interfaces in the parent class become private, of course, only implementation inheritance can be achieved) ), the effect of interface inheritance is simulated through pure abstract base class, but in C++, pure virtual function can also provide default implementation, so this is impure interface inheritance, but in Java we can get real interface inheritance with interface .

You may also like...

Leave a Reply

Your email address will not be published.