c++设计模式之适配器模式

  1. 适配器模式(Adapter Pattern)
    定义
    适配器模式的目的是让不兼容的接口能够协同工作。通过定义一个适配器类,将原本接口不兼容的两种类的接口转化为一致的接口,使得原本无法交互的类可以互操作。

应用场景

当你希望将一些已经存在的类、接口或服务纳入到现有的系统中,但它们的接口与系统不兼容时,可以使用适配器模式。

例如,使用不同的外部库(如不同的交换机设备、数据库等),它们的接口可能不一致,这时可以通过适配器模式来统一接口。

结构

Target(目标接口):客户希望使用的接口。

Adapter(适配器):实现目标接口,通过适配已有的接口将不同的接口适配成统一的接口。

Adaptee(被适配者):需要适配的类或接口,通常是已经存在的类或库。

代码示例

假设我们有一个监控系统,需要与多种交换机设备(比如 S5730 和 S12700)进行通信,但它们的接口不一致。通过适配器模式,可以将它们统一为一个标准接口,使得监控系统能够统一管理这些设备。

1.1 定义目标接口

cpp 复制代码
cpp
// SwitchInterface.h
#ifndef SWITCH_INTERFACE_H
#define SWITCH_INTERFACE_H

class SwitchInterface {
public:
    virtual void login(const std::string& username, const std::string& password) = 0;
    virtual void configure(const std::string& config) = 0;
    virtual ~SwitchInterface() {}
};

#endif // SWITCH_INTERFACE_H
SwitchInterface 定义了监控系统期望的统一接口,所有的交换机设备都需要实现这个接口。

1.2 定义具体设备类
cpp
// S5730.h
#ifndef S5730_H
#define S5730_H

#include <iostream>

class S5730 {
public:
    void loginS5730(const std::string& username, const std::string& password) {
        std::cout << "S5730 login with username: " << username << std::endl;
    }

    void configureS5730(const std::string& config) {
        std::cout << "Configuring S5730 with: " << config << std::endl;
    }
};

#endif // S5730_H
cpp
// S12700.h
#ifndef S12700_H
#define S12700_H

#include <iostream>

class S12700 {
public:
    void loginS12700(const std::string& username, const std::string& password) {
        std::cout << "S12700 login with username: " << username << std::endl;
    }

    void configureS12700(const std::string& config) {
        std::cout << "Configuring S12700 with: " << config << std::endl;
    }
};

#endif // S12700_H

1.3 定义适配器
cpp
// S5730Adapter.h
#ifndef S5730_ADAPTER_H
#define S5730_ADAPTER_H

#include "SwitchInterface.h"
#include "S5730.h"

class S5730Adapter : public SwitchInterface {
private:
    S5730 s5730;
public:
    void login(const std::string& username, const std::string& password) override {
        s5730.loginS5730(username, password);
    }

    void configure(const std::string& config) override {
        s5730.configureS5730(config);
    }
};

#endif // S5730_ADAPTER_H
cpp

// S12700Adapter.h
#ifndef S12700_ADAPTER_H
#define S12700_ADAPTER_H

#include "SwitchInterface.h"
#include "S12700.h"

class S12700Adapter : public SwitchInterface {
private:
    S12700 s12700;
public:
    void login(const std::string& username, const std::string& password) override {
        s12700.loginS12700(username, password);
    }

    void configure(const std::string& config) override {
        s12700.configureS12700(config);
    }
};

#endif // S12700_ADAPTER_H
1.4 使用适配器
cpp
// Main.cpp
#include "S5730Adapter.h"
#include "S12700Adapter.h"

int main() {
    SwitchInterface* switch1 = new S5730Adapter();
    switch1->login("admin", "password123");
    switch1->configure("Set interface GigabitEthernet0/0/1 to up");

    SwitchInterface* switch2 = new S12700Adapter();
    switch2->login("admin", "password456");
    switch2->configure("Set interface GigabitEthernet0/0/2 to down");

    delete switch1;
    delete switch2;

    return 0;
}
复制代码
解释
SwitchInterface 定义了监控系统希望交互的接口。
S5730 和 S12700 是具体的交换机类,它们的接口与 SwitchInterface 不兼容。
S5730Adapter 和 S12700Adapter 是适配器,它们实现了 SwitchInterface 接口,并将调用委托给具体的设备类。
监控系统通过 SwitchInterface 接口与交换机交互,而不需要关心具体交换机的实现细节。
优点
通过适配器模式,监控软件能够与多个不同型号的交换机设备兼容。
添加新型号时,只需要为新型号实现一个适配器类,而无需修改现有的监控系统代码。
相关推荐
研究点啥好呢2 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
_dindong2 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
沫璃染墨2 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
6Hzlia3 小时前
【Hot 100 刷题计划】 LeetCode 17. 电话号码的字母组合 | C++ 回溯算法经典模板
c++·算法·leetcode
计算机安禾4 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio
unicrom_深圳市由你创科技4 小时前
做虚拟示波器这种实时波形显示的上位机,用什么语言?
c++·python·c#
无限进步_4 小时前
【C++】电话号码的字母组合:从有限处理到通用解法
开发语言·c++·ide·windows·git·github·visual studio
C++ 老炮儿的技术栈4 小时前
GCC编译时无法向/tmp 目录写入临时汇编文件,因为设备空间不足,解决
linux·运维·开发语言·汇编·c++·git·qt
橘颂TA4 小时前
【笔试】算法的暴力美学——牛客 NC213140 :除2!
c++·算法·结构与算法
wsoz5 小时前
Leetcode普通数组-day5、6
c++·算法·leetcode·数组