设计模式——适配器模式

适配器模式,是一种结构型设计模式,关键就是适配。举一个常见的例子。现在的大部分手机都是typec接口,但是依然有很多用户的安卓手机是旧的USB接口,想要匹配用户的需求就需要一个转接头(适配器),然后才能更好的匹配用户的需求。

可能有人会问,为啥不能干脆换一个typec的充电线就可以了,实际操作确实可以,但是在软件开发领域我们都知道开闭原则。如果直接换数据线就好比直接修改老代码,这样不仅会丢掉原来的功能,还可能会接口不协调影响整体,所以加一个转接头(适配器)更安全也更合理。

这样,就可以分我以下几个基本角色:

  • 目标接口Target:客户端希望使用的接口;
  • 适配器类Adapter:实现客户端目标接口,持有一个需要适配的类实例
  • 被适配者Adaptee:需要被适配的类

这样,客户端就可以使用目标接口而不要对原来的Adaptee进行修改,Adapter起到一个转接拓展的功能。

基本实现

cpp 复制代码
#include<bits/stdc++.h>
// 目标接口
class Target {

    public:

    virtual ~Target() = default;

    virtual std::string Request() const {

        return "Target: The default target's behevior.";

    }

  

};

  
  

// 被适配类

class Adaptee {

    public:

    std::string SpecificRequest() const {

        return ".eetpadA eht fo roivaheb laiceps";

    }

  

};


  

// 适配器类

class Adapter :public Target, public Adaptee {

    public:

    Adapter() {}

    std::string Request() const {

        std::string to_reverse = SpecificRequest();

        std::reverse(to_reverse.begin(), to_reverse.end());

        return "Adapter:(TRANSLATED) " + to_reverse;

    }

};

  
  

// 客户端类

void ClientCode(const Target* target) {

    std::cout << target->Request() << std::endl;

}

  

int main() {

    using namespace std;

    cout << "Client: I can work just fine with the Target object:\n";

    Target* target = new Target;

    ClientCode(target);

    cout << endl;

  

    Adaptee* adaptee = new Adaptee;

    cout << "Client : The Adaptee class has a weird interface.\n";

    cout << "Adaptee: " << adaptee->SpecificRequest() << endl;

    cout << endl;

    cout << "Client :But I can work with it via the Adapter:\n";

    Adapter* adapter = new Adapter;

    ClientCode(adapter);

    delete target;

    delete adaptee;

    delete adapter;

 
    return 0;

}

输出:

复制代码
Client: I can work just fine with the Target object:
Target: The default target's behevior.

Client : The Adaptee class has a weird interface.
Adaptee: .eetpadA eht fo roivaheb laiceps

Client :But I can work with it via the Adapter:
Adapter:(TRANSLATED) special behavior of the Adaptee.

如图所示,适配器类的主要目的就是将被适配类的对象就行转换,这里是做了最简单的反转操作,然后返回给客户端。可以到这里使用了C++的双重继承属性实现起来比较方便。否则适配器就只能继承目标类,然后在其中定义一个被适配者实例,再调用其方法。

实际开发过程上,适配器模式往往扮演一个"补救"和"拓展"的角色:

  • 当使用一个已经存在的类,但是他的接口与你的代码不兼容时,可以使用适配器模式
  • 当系统扩展阶段需要增加新的类时,并且类的接口和系统现有的类不一致时,可以使用。

使用适配器模式可以将客户端代码与具体的类解耦,客户端不需要知道被适配者的细节,客户端代码也不需要修改,这使得他具有良好的拓展性,但是这也势必导致系统的复杂性。

相关推荐
Bella的成长园地6 小时前
面试中关于 c++ async 的高频面试问题有哪些?
c++·面试
彷徨而立7 小时前
【C/C++】什么是 运行时库?运行时库 /MT 和 /MD 的区别?
c语言·c++
qq_417129257 小时前
C++中的桥接模式变体
开发语言·c++·算法
No0d1es9 小时前
电子学会青少年软件编程(C语言)等级考试试卷(三级)2025年12月
c语言·c++·青少年编程·电子学会·三级
bjxiaxueliang10 小时前
一文掌握C/C++命名规范:风格、规则与实践详解
c语言·开发语言·c++
xu_yule11 小时前
网络和Linux网络-13(高级IO+多路转接)五种IO模型+select编程
linux·网络·c++·select·i/o
2301_7657031411 小时前
C++与自动驾驶系统
开发语言·c++·算法
轩情吖11 小时前
Qt的窗口(三)
c++·qt
热爱编程的小刘11 小时前
Lesson04---类与对象(下篇)
开发语言·c++·算法
郝学胜-神的一滴12 小时前
Linux网络编程之listen函数:深入解析与应用实践
linux·服务器·开发语言·网络·c++·程序人生