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 接口与交换机交互,而不需要关心具体交换机的实现细节。
优点
通过适配器模式,监控软件能够与多个不同型号的交换机设备兼容。
添加新型号时,只需要为新型号实现一个适配器类,而无需修改现有的监控系统代码。
相关推荐
渡我白衣20 分钟前
字符串的陷阱与艺术——std::string全解析
网络·c++·人工智能·自然语言处理·智能路由器·信息与通信·caffe
吃不饱的得可可24 分钟前
C++17常用新特性
开发语言·c++
_OP_CHEN31 分钟前
算法基础篇:(七)基础算法之二分算法 —— 从 “猜数字” 到 “解难题” 的高效思维
c++·算法·蓝桥杯·二分查找·acm·二分答案·二分算法
一匹电信狗35 分钟前
【C++11】Lambda表达式+新的类功能
服务器·c++·算法·leetcode·小程序·stl·visual studio
煤球王子1 小时前
学而时习之:C++中的枚举
c++
楼田莉子1 小时前
C++/Linux小项目:自主shell命令解释器
linux·服务器·开发语言·c++·后端·学习
昨天的猫1 小时前
《拒绝重复代码!模板模式教你优雅复用算法骨架》
后端·设计模式
L.EscaRC1 小时前
ArkTS分布式设计模式浅析
分布式·设计模式·arkts
草莓火锅2 小时前
用c++求第n个质数
开发语言·c++·算法
GISer_Jing2 小时前
OSG多视口与多通道渲染核心技术解析
c++·3d·新浪微博