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 接口与交换机交互,而不需要关心具体交换机的实现细节。
优点
通过适配器模式,监控软件能够与多个不同型号的交换机设备兼容。
添加新型号时,只需要为新型号实现一个适配器类,而无需修改现有的监控系统代码。
相关推荐
noipp35 分钟前
推荐题目:洛谷 P6231 [JSOI2013] 公交系统
c语言·数据结构·c++·算法·游戏·洛谷·luogu
c238561 小时前
C/C++每日一练6
c语言·c++·算法
念恒123061 小时前
网络基础
linux·网络·c++
mct1232 小时前
c++ iconv 字符utf-8转换gb2312失败
开发语言·c++
ziguo11222 小时前
Windows API 文件操作超级指南——从入门到内核
c++·windows·visualstudio
zhangfeng11334 小时前
Ascendc 大语言模型框架 AscendCraft 和pypto 的区别 pypto生成算子c++源码吗
c++·人工智能·语言模型·算子开发
一只小灿灿4 小时前
C++ 修饰符全面详解
开发语言·c++
code_pgf4 小时前
C/C++ 中 `typedef` 关键字详解
c语言·c++
会周易的程序员5 小时前
js-shm: 高性能 Node.js 共享内存模块
开发语言·javascript·c++·node.js·共享内存·shm
2023自学中5 小时前
imx6ull 开发板 贪吃蛇, C++11 SDL2 无硬件GPU优化版
linux·c++