C++设计模式之适配器

动机

在软件系统中,由于应用环境的变化,常常需要将"一些现存的对象"放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足的。

如何应对这种"迁移的变化"?如何既能利用现有对象的良好实现,同时又能满足新的应用环境所要求的接口?

代码示例

cpp 复制代码
//目标接口(新接口)
class ITarget{
public:
    virtual void process()=0;
};

//遗留接口(老接口)
class IAdaptee{
public:
    virtual void foo(int data)=0;
    virtual int bar()=0;
};

//遗留类型
class OldClass: public IAdaptee{
    //....
};

//对象适配器
class Adapter: public ITarget{ //继承
protected:
    IAdaptee* pAdaptee;//组合
    
public:
    
    Adapter(IAdaptee* pAdaptee){
        this->pAdaptee=pAdaptee;
    }
    
    virtual void process(){
        int data=pAdaptee->bar();
        pAdaptee->foo(data);        
    }     
};


//类适配器
class Adapter: public ITarget,
               protected OldClass{ //多继承
                             
}


int main(){
    IAdaptee* pAdaptee=new OldClass();
      
    ITarget* pTarget=new Adapter(pAdaptee);
    pTarget->process();    
}


class stack{
    deque container;
    
};

class queue{
    deque container;
    
};

适配器模式定义

将一个类的接口转换成客户希望的另外一个接口。 Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

要点总结

Adapter模式主要应用于"希望复用一些现存的类,但是接口又与复用环境要求不一致的情况",在遗留代码复用、类库迁移等方面非常有用。

GoF23定义了两种Adapter模式的实现结构:对象适配器和类适配器。但类适配器采用多继承"的实现方式,一般不推荐使用。对象适配器采用"对象组合"的方式,更符合松耦合精神。

Adapter模式可以实现的非常灵活,不必拘泥于Gof23中定义的两种结构。例如,完全可以将Adapter模式中的"现存对象"作为新的接口方法参数,来达到适配的目的。

chatGPT给出的adapter例子

下面是一个简单的C++ Adapter 模式的例子,假设有一个旧的类 OldClass,其接口不符合新的需求,然后通过适配器 Adapter 将其适配为符合新需求的接口:

cpp 复制代码
#include <iostream>

// 旧的类,接口不符合新需求
class OldClass {
public:
    void legacyMethod() {
        std::cout << "Legacy method of OldClass" << std::endl;
    }
};

// 新的接口,符合新需求
class NewInterface {
public:
    virtual void newMethod() = 0;
    virtual ~NewInterface() {}
};

// 适配器,将OldClass适配为NewInterface
class Adapter : public NewInterface {
private:
    OldClass oldInstance;

public:
    void newMethod() override {
        // 在这里调用OldClass的方法,以适应新的接口
        oldInstance.legacyMethod();
    }
};

// 客户端代码,使用新的接口
void clientCode(NewInterface* newObject) {
    newObject->newMethod();
}

int main() {
    // 使用适配器将OldClass适配为NewInterface
    Adapter adapter;

    // 客户端代码使用新的接口
    clientCode(&adapter);

    return 0;
}

在这个例子中,OldClass 是一个旧的类,具有 legacyMethod 方法,但其接口不符合 NewInterface 的新需求。通过创建一个适配器类 Adapter,它继承了 NewInterface 并持有一个 OldClass 的实例,将 legacyMethod 适配为 newMethod。最后,在客户端代码中,我们可以使用 NewInterface 的接口,而实际上调用了 OldClass 的方法。这就是 Adapter 模式的作用。

相关推荐
西西弗Sisyphus11 分钟前
C++ explicit 关键字工程实践
开发语言·c++
郝学胜-神的一滴2 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
feixing_fx2 小时前
原始类型与引用类型:赋值时到底复制了什么
开发语言·前端·javascript·前端框架
wuyk5552 小时前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
AIgorithmGEEK3 小时前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
神明不懂浪漫3 小时前
【第八章】多线程(一)
java·开发语言·经验分享·笔记
yiangL_4 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互
一拳一个呆瓜5 小时前
【STL】头文件:<cmath>
c++·stl
草莓熊Lotso5 小时前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构
慧都产品智能化6 小时前
Qt 开发 CAD/CAM 工序表的隐性成本分析
c++·qt·qt5·qt6.3