适配器模式(大话设计模式)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;
}
相关推荐
寻找码源36 分钟前
【头歌实训:利用kmp算法求子串在主串中不重叠出现的次数】
c语言·数据结构·算法·字符串·kmp
闲人一枚(学习中)1 小时前
设计模式-创建型-抽象工厂模式
设计模式·抽象工厂模式
带多刺的玫瑰2 小时前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
陌小呆^O^3 小时前
Cmakelist.txt之win-c-udp-server
c语言·开发语言·udp
时光の尘3 小时前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
-一杯为品-3 小时前
【51单片机】程序实验5&6.独立按键-矩阵按键
c语言·笔记·学习·51单片机·硬件工程
小白不太白9504 小时前
设计模式之 观察者模式
观察者模式·设计模式
爱摸鱼的孔乙己4 小时前
【数据结构】链表(leetcode)
c语言·数据结构·c++·链表·csdn
Dola_Pan4 小时前
C语言:数组转换指针的时机
c语言·开发语言·算法
IU宝5 小时前
C/C++内存管理
java·c语言·c++