适配器模式(大话设计模式)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;
}
相关推荐
捕鲸叉22 分钟前
MVC(Model-View-Controller)模式概述
开发语言·c++·设计模式
wrx繁星点点38 分钟前
享元模式:高效管理共享对象的设计模式
java·开发语言·spring·设计模式·maven·intellij-idea·享元模式
凉辰44 分钟前
设计模式 策略模式 场景Vue (技术提升)
vue.js·设计模式·策略模式
菜菜-plus1 小时前
java设计模式之策略模式
java·设计模式·策略模式
暗黑起源喵1 小时前
设计模式-迭代器
设计模式
小林熬夜学编程2 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法
墨墨祺2 小时前
嵌入式之C语言(基础篇)
c语言·开发语言
躺不平的理查德2 小时前
数据结构-链表【chapter1】【c语言版】
c语言·开发语言·数据结构·链表·visual studio
lexusv8ls600h3 小时前
微服务设计模式 - 网关路由模式(Gateway Routing Pattern)
spring boot·微服务·设计模式
幼儿园园霸柒柒3 小时前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节