适配器模式(大话设计模式)C/C++版本

适配器模式

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;
}
相关推荐
Damon_X8 分钟前
桥接模式(Bridge Pattern)
设计模式·桥接模式
爱吃西瓜的小菜鸡3 小时前
【C语言】判断回文
c语言·学习·算法
越甲八千4 小时前
重温设计模式--享元模式
设计模式·享元模式
FeboReigns5 小时前
C++简明教程(文章要求学过一点C语言)(1)
c语言·开发语言·c++
FeboReigns5 小时前
C++简明教程(文章要求学过一点C语言)(2)
c语言·开发语言·c++
码农爱java6 小时前
设计模式--抽象工厂模式【创建型模式】
java·设计模式·面试·抽象工厂模式·原理·23种设计模式·java 设计模式
_小柏_6 小时前
C/C++基础知识复习(43)
c语言·开发语言·c++
yoyobravery6 小时前
c语言大一期末复习
c语言·开发语言·算法
越甲八千6 小时前
重温设计模式--中介者模式
windows·设计模式·中介者模式
犬余7 小时前
设计模式之桥接模式:抽象与实现之间的分离艺术
笔记·学习·设计模式·桥接模式