适配器模式

理论

适配器模式(Adapter),将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

适配器模式可分为两种类型:类适配器模式和对象适配器模式。这里是以对象适配器为例。

适配器模式的应用场景:

系统的数据和行为都正确,但接口不符时,考虑使用适配器。目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。

(如果能事先预防接口不同的问题,不匹配问题就不会发生。在有小的接口不统一问题发生时,应该及时重构使问题不至于扩大。只有碰到无法改变原有设计和代码的情况才考虑适配。)

实例

模式教练暂停时给后卫、中锋、前锋分配进攻和防守任务。外籍中锋听不懂外语,需要翻译(在外籍中锋一时学不会英语,教练也一时学不会汉语的情况下使用)

UML类图

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;
 
//Target
class Player {
public:
    Player(string _name) {
        name = _name;
    }
 
    virtual void Attack() = 0;
    virtual void Defense() = 0;
     
protected:
    string name;
};
 
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;
    }
};
 
//Adaptee
class ForeignCenter {
public:
    void SetName(string _name) {
        name = _name;
    }
    string GetName() {
        return name;
    }
    void ForeignAttack() {
        cout << "外籍中锋 " << name << " 进攻" << endl;
    }
    void ForeignerDefense() {
        cout << "外籍中锋 " << name << " 防守" << endl;
    }
 
private:
    string name;
};
 
//适配器类 Adapter
class Translator :public Player {
public:
    Translator(string _name) :Player(_name) {
        wjzf = new ForeignCenter();
        wjzf->SetName(_name);
    }
    virtual void Attack() {
        wjzf->ForeignAttack();
    }
    virtual void Defense() {
        wjzf->ForeignerDefense();
    }
    ~Translator() {
        if (wjzf != NULL) {
            delete wjzf;
        }
    }
 
private:
    ForeignCenter* wjzf;
};
 
int main()
{
    Player* b = new Forwards("巴蒂尔");
    b->Attack();
    Player* m = new Guards("麦克格雷迪");
    m->Attack();
    Player* ym = new Translator("姚明");
    ym->Attack();
    ym->Defense();
 
    delete b;
    delete m;
    delete ym;
 
    system("pause");
    return 0;
}

运行结果

 

posted @   KYZH  阅读(277)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示

目录导航