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 接口与交换机交互,而不需要关心具体交换机的实现细节。
优点
通过适配器模式,监控软件能够与多个不同型号的交换机设备兼容。
添加新型号时,只需要为新型号实现一个适配器类,而无需修改现有的监控系统代码。
相关推荐
笨手笨脚の12 分钟前
设计模式-策略模式
设计模式·策略模式·行为型设计模式
澄澈i17 分钟前
CMake学习篇[3]---CMake进阶+嵌套CMakeLists+多层级关系
c++·学习·cmake
王嘉俊92540 分钟前
设计模式--适配器模式:优雅解决接口不兼容问题
java·设计模式·适配器模式
王嘉俊92542 分钟前
设计模式--组合模式:统一处理树形结构的优雅设计
java·设计模式·组合模式
rongqing201943 分钟前
Google 智能体设计模式:多智能体协作
设计模式
杨小码不BUG1 小时前
蛇形舞动:矩阵填充的艺术与算法(洛谷P5731)
c++·算法·矩阵·csp-j/s·循环控制
草莓熊Lotso2 小时前
《C++ STL list 完全指南:从基础操作到特性对比,解锁链表容器高效用法》
开发语言·c++·list
繁星星繁3 小时前
C++11学习笔记
c++·笔记·学习
Stanford_11068 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
千里马-horse9 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner