适配器模式
C++
cpp
#include <iostream>
using namespace std;
// 球员
class Player
{
protected:
string name;
public:
Player(string name) : name(name) {}
virtual void Attack() = 0;
virtual void Defense() = 0;
virtual ~Player() {}
};
// 前锋
class Forwards : public Player
{
public:
Forwards(string name) : Player(name) {}
virtual void Attack()
{
cout << "前锋 " << name << "进攻" << endl;
}
virtual void Defense()
{
cout << "前锋 " << name << "防守" << endl;
}
};
// 中锋
class Center : public Player
{
public:
Center(string name) : Player(name) {}
virtual void Attack()
{
cout << "中锋 " << name << "进攻" << endl;
}
virtual void Defense()
{
cout << "中锋 " << name << "防守" << endl;
}
};
// 后卫
class Guards : public Player
{
public:
Guards(string name) : Player(name) {}
virtual void Attack()
{
cout << "后卫 " << name << "进攻" << endl;
}
virtual void Defense()
{
cout << "后卫 " << name << "防守" << endl;
}
};
class ForeignCenter
{
public:
void SetName(string name)
{
this->name = name;
}
string GstName()
{
return name;
}
void ForeignAttack()
{
cout << "外籍中锋 " << name << " 进攻" << endl;
}
void ForeignDefense()
{
cout << "外籍中锋 " << name << " 防守" << endl;
}
private:
string name;
};
// 适配器类 翻译者
class Translator : public Player
{
public:
Translator(string name) : Player(name)
{
ym = new ForeignCenter;
ym->SetName(name);
}
~Translator()
{
if (ym != NULL)
delete ym;
}
void Attack()
{ // 翻译者将Attack 翻译成 ForeignAttack
ym->ForeignAttack();
}
void Defense()
{
ym->ForeignDefense(); // 翻译者将Defense 翻译成 ForeignDefense
}
private:
ForeignCenter *ym; // 外籍中锋
};
int main()
{
Player *b = new Forwards("巴蒂尔");
b->Attack();
Player *c = new Guards("麦克格雷迪");
c->Attack();
Player *ym = new Translator("姚明"); // 姚明问: "Attack和Defense是什么意思?"
ym->Attack();
ym->Defense();
return 0;
}
C
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Player
{
char name[50];
void (*attack)(const char *);
void (*defense)(const char *);
} Player;
typedef struct Forwards
{
Player base;
} Forwards;
typedef struct Center
{
Player base;
} Center;
typedef struct Guards
{
Player base;
} Guards;
typedef struct ForeignCenter
{
char name[50];
} ForeignCenter;
typedef struct Translator
{
Player base;
ForeignCenter *foreignCenter;
} Translator;
void forwards_attack(const char *name)
{
printf("前锋 %s 进攻\n", name);
}
void forwards_defense(const char *name)
{
printf("前锋 %s 防守\n", name);
}
void center_attack(const char *name)
{
printf("中锋 %s 进攻\n", name);
}
void center_defense(const char *name)
{
printf("中锋 %s 防守\n", name);
}
void guards_attack(const char *name)
{
printf("后卫 %s 进攻\n", name);
}
void guards_defense(const char *name)
{
printf("后卫 %s 防守\n", name);
}
void foreign_center_attack(ForeignCenter *fc)
{
printf("外籍中锋 %s 进攻\n", fc->name);
}
void foreign_center_defense(ForeignCenter *fc)
{
printf("外籍中锋 %s 防守\n", fc->name);
}
Player *create_forwards(const char *name)
{
Player *p = (Player *)malloc(sizeof(Player));
strcpy(p->name, name);
p->attack = forwards_attack;
p->defense = forwards_defense;
return p;
}
Player *create_center(const char *name)
{
Player *p = (Player *)malloc(sizeof(Player));
strcpy(p->name, name);
p->attack = center_attack;
p->defense = center_defense;
return p;
}
Player *create_guards(const char *name)
{
Player *p = (Player *)malloc(sizeof(Player));
strcpy(p->name, name);
p->attack = guards_attack;
p->defense = guards_defense;
return p;
}
Translator *create_translator(const char *name)
{
Translator *t = (Translator *)malloc(sizeof(Translator));
strcpy(t->base.name, name);
t->foreignCenter = (ForeignCenter *)malloc(sizeof(ForeignCenter));
strcpy(t->foreignCenter->name, name);
t->base.attack = (void (*)(const char *))foreign_center_attack;
t->base.defense = (void (*)(const char *))foreign_center_defense;
return t;
}
void player_free(Player *p)
{
free(p);
}
void translator_free(Translator *t)
{
if (t->foreignCenter)
free(t->foreignCenter);
free(t);
}
int main()
{
Player *b = create_forwards("巴蒂尔");
b->attack(b->name);
Player *c = create_guards("麦克格雷迪");
c->attack(c->name);
Translator *ym = create_translator("姚明");
ym->base.attack(ym->base.name);
ym->base.defense(ym->base.name);
player_free(b);
player_free(c);
translator_free(ym);
return 0;
}