/*
* 适配器模式
* 适用于双方都不适合修改的时候
*/
class Target {
public:
virtual void request() {
std::cout << "Target request." << std::endl;
}
};
class Adaptee {
public:
virtual void specialRequest() {
std::cout << "Adaptee specialRequest." << std::endl;
}
};
class Adapter: public Target {
public:
Adapter() {
this->adaptee = new Adaptee();
}
virtual ~Adapter() {
delete this->adaptee;
}
virtual void request() {
Target::request();
this->adaptee->specialRequest();
}
private:
Adaptee* adaptee;
};
int main() {
Adapter* adapter = new Adapter();
adapter->request();
delete adapter;
return 0;
}
#include <thread>
#include <iostream>
#include <vector>
using namespace std;
class Player {
public:
Player(string name): name(name) {}
virtual ~Player() = default;
virtual void attack() = 0;
virtual void deffence() = 0;
protected:
string name;
};
class Forwards: public Player {
public:
Forwards(string name): Player(name) {}
virtual void attack() {
std::cout << this->name << " Forwards attack." << std::endl;
}
virtual void deffence() {
std::cout << this->name << " Forwards deffence." << std::endl;
}
};
class Center: public Player {
public:
Center(string name): Player(name) {}
virtual void attack() {
std::cout << this->name << " Center attack." << std::endl;
}
virtual void deffence() {
std::cout << this->name << " Center deffence." << std::endl;
}
};
class Translater: public Player {
public:
Translater(string name): Player(name) {
this->player = new Center(name);
}
virtual ~Translater() {
delete this->player;
}
virtual void attack() {
this->player->attack();
}
virtual void deffence() {
this->player->deffence();
}
private:
Player* player;
};
int main() {
Player* player = new Translater("Ronaldo");
player->attack();
player->deffence();
delete player;
}