《设计模式的艺术》笔记 - 适配器模式

这篇具有很好参考价值的文章主要介绍了《设计模式的艺术》笔记 - 适配器模式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

介绍

        适配器模式将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作,其别名为包装器。

实现

对象适配器模式

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>

class Target {  // 目标类
public:
    virtual void request() = 0;
};

class Adaptee {     // 适配者类
public:
    void specificRequest();
};

class Adapter : public Target { // 适配器类
public:
    Adapter(Adaptee *adaptee);
    void request() override;

private:
    Adaptee *m_adaptee;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

Adapter::Adapter(Adaptee *adaptee) {
    m_adaptee = adaptee;
}

void Adapter::request() {
    if (m_adaptee) {
        m_adaptee->specificRequest();
    }
}

void Adaptee::specificRequest() {
    std::cout << "Adaptee被适配" << std::endl;
}

main.cpp

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Adaptee *adaptee = new Adaptee;
    Target *target = new Adapter(adaptee);
    target->request();
    delete target;
    delete adaptee;

    return 0;
}

类适配器模式

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>

class Target {  // 目标类
public:
    virtual void request() = 0;
};

class Adaptee {     // 适配者类
public:
    void specificRequest();
};

class Adapter : public Target, public Adaptee { // 适配器类
public:
    void request() override;

};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"


void Adapter::request() {
    specificRequest();
}

void Adaptee::specificRequest() {
    std::cout << "Adaptee被适配" << std::endl;
}

main.cpp

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Target *target = new Adapter();
    target->request();
    delete target;

    return 0;
}

双向适配器

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>

class Target {  // 目标类
public:
    void request();
};

class Adaptee {     // 适配者类
public:
    void specificRequest();
};

class Adapter { // 适配器类
public:
    Adapter(Target *target);
    Adapter(Adaptee *adaptee);
    void request();
    void specificRequest();

private:
    Target *m_target;
    Adaptee *m_adaptee;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

void Target::request() {
    std::cout << "Target被适配" << std::endl;
}

Adapter::Adapter(Target *target) {
    m_target = target;
}

Adapter::Adapter(Adaptee *adaptee) {
    m_adaptee = adaptee;
}

void Adapter::request() {
    if (m_adaptee) {
        m_adaptee->specificRequest();
    }
}

void Adapter::specificRequest() {
    if (m_target) {
        m_target->request();
    }
}

void Adaptee::specificRequest() {
    std::cout << "Adaptee被适配" << std::endl;
}

main.cpp

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Target *target = new Target();
    Adaptee *adaptee = new Adaptee();
    Adapter *adapter = new Adapter(target);
    adapter->specificRequest();
    delete adapter;
    adapter = new Adapter(adaptee);
    adapter->request();
    delete adapter;
    delete adaptee;
    delete target;

    return 0;
}

缺省适配器模式

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>

class ServiceInterface {    // 适配者接口
public:
    virtual void request() = 0;
    virtual void request2() = 0;
    virtual void request3() = 0;
};

class AbstractService : public ServiceInterface {     // 缺省适配器类
public:
    virtual void request() override;

    virtual void request2() override;

    virtual void request3() override;
};

class Adaptee {     // 适配者类
public:
    void specificRequest();
};

class Adapter : public AbstractService { // 具体适配器类
public:
    Adapter(Adaptee *adaptee);
    void request() override;

private:
    Adaptee *m_adaptee;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

Adapter::Adapter(Adaptee *adaptee) {
    m_adaptee = adaptee;
}

void Adapter::request() {
    if (m_adaptee) {
        m_adaptee->specificRequest();
    }
}

void Adaptee::specificRequest() {
    std::cout << "Adaptee被适配" << std::endl;
}

void AbstractService::request() {
    std::cout << "缺省函数request" << std::endl;
}

void AbstractService::request2() {
    std::cout << "缺省函数request2" << std::endl;
}

void AbstractService::request3() {
    std::cout << "缺省函数request3" << std::endl;
}

main.cpp

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Adaptee *adaptee = new Adaptee();
    Adapter *adapter = new Adapter(adaptee);
    adapter->request();
    adapter->request2();
    adapter->request3();
    delete adapter;
    delete adaptee;

    return 0;
}

总结

优点

        1. 将目标类和适配者类解耦。通过引入一个适配器类来重用现有的适配者类,无须修改原有结构。

        2. 增加了类的透明性和复用性。将具体的业务实现过程封装在适配者类中,对于客户端类而言是透明的,而且提高了适配者类的复用性,同一个适配者类可以在多个不同的系统中复用。

        3. 灵活性和扩展性都非常好。通过使用配置文件,可以很方便地更换适配器,也可以在不修改原有代码的基础上增加新的适配器类,完全符合开闭原则。

缺点

        1. 对于Java、C#等不支持多重类继承的语言,一次最多只能适配一个适配者类,不能同时适配多个适配者。

        2. 适配者类不能为最终类,例如在Java中不能为final类,C#中不能为sealed类。

        3. 在Java、C#等语言中,类适配器模式中的目标抽象类只能为接口,不能为类,其使用有一定的局限性。

适用场景

        1. 系统需要使用一些现有的类,而这些类的接口(例如方法名)不符合系统的需要,甚至没有这些类的源代码。

        2. 想创建一个可以重复使用的类,用于与一些彼此之间没有太大关联的类,包括一些可能在将来引进的类一起工作。

练习

对象适配器模式

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>

class Encryption {  // 目标类
public:
    virtual std::string encrypt(std::string data) = 0;
};

class ThirdLib {     // 适配者类
public:
    std::string specificEncrypt(std::string data);
    std::string specificDecrypt(std::string data);
};

class AdapterEncyption : public Encryption {    // 适配器类
public:
    AdapterEncyption(ThirdLib *thirdLib);
    ~AdapterEncyption();
    std::string encrypt(std::string data) override;

private:
    ThirdLib *m_thirdLib;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

AdapterEncyption::AdapterEncyption(ThirdLib *thirdLib) {
    m_thirdLib = thirdLib;
}

AdapterEncyption::~AdapterEncyption() {
    if (m_thirdLib) {
        delete m_thirdLib;
    }
}

std::string AdapterEncyption::encrypt(std::string data) {
    if (m_thirdLib) {
        return m_thirdLib->specificEncrypt(data);
    }
    return data;
}

std::string ThirdLib::specificEncrypt(std::string data) {
    return "加密后的" + data;
}

std::string ThirdLib::specificDecrypt(std::string data) {
    return "解密后的" + data;
}

main.cpp

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    ThirdLib *thirdLib = new ThirdLib();
    Encryption *encryption = new AdapterEncyption(thirdLib);
    std::string data = encryption->encrypt("口令");
    std::cout << data << std::endl;
    data = encryption->encrypt("邮箱");
    std::cout << data << std::endl;

    return 0;
}

类适配器模式

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>

class Encryption {  // 目标类
public:
    virtual std::string encrypt(std::string data) = 0;
};

class ThirdLib {     // 适配者类
public:
    std::string specificEncrypt(std::string data);
    std::string specificDecrypt(std::string data);
};

class AdapterEncyption : public Encryption, public ThirdLib {    // 适配器类
public:
    AdapterEncyption();
    ~AdapterEncyption();
    std::string encrypt(std::string data) override;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

AdapterEncyption::AdapterEncyption() {

}

AdapterEncyption::~AdapterEncyption() {

}

std::string AdapterEncyption::encrypt(std::string data) {
    return specificEncrypt(data);
}

std::string ThirdLib::specificEncrypt(std::string data) {
    return "加密后的" + data;
}

std::string ThirdLib::specificDecrypt(std::string data) {
    return "解密后的" + data;
}

main.cpp文章来源地址https://www.toymoban.com/news/detail-801836.html

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Encryption *encryption = new AdapterEncyption();
    std::string data = encryption->encrypt("口令");
    std::cout << data << std::endl;
    data = encryption->encrypt("邮箱");
    std::cout << data << std::endl;
    delete encryption;

    return 0;
}

到了这里,关于《设计模式的艺术》笔记 - 适配器模式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 设计模式--适配器模式

    目录 基本介绍 工作原理 类适配模式 介绍 应用实例介绍 类适配器模式注意事项和细节 对象适配模式 介绍 对象适配器模式注意事项和细节 接口适配器模式 介绍 适配器模式的注意事项和细节  (1) 适配器模式(Adapter Pattern) 将某个类的接口转换成客户端期望的另一个接口表示

    2023年04月26日
    浏览(33)
  • 设计模式——适配器模式

    说起适配器其实在我们的生活中是非常常见的,比如:学校的宿舍的电压都比较低,而有的学生想使用大功率电器,宿舍的就会跳闸,然而如果你使用一个适配器(变压器)就可以使用了(温馨提示宿舍使用大功率电器不太安全,容易引起火灾,希望大家谨慎使用)。 又比如

    2024年02月12日
    浏览(47)
  • 《设计模式》之适配器模式

    把一个类的接口转换成客户端所期待的另一种接口,从而使原接口不匹配而无法再一起工作的两个类能在一起工作。 在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象”放在新的环境中应用,但是新环境要求的接口是这些现存对象所不能满足的。 如何应对

    2024年02月09日
    浏览(40)
  • 设计模式-- 3.适配器模式

    将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 请求者(client):客户端角色,需要使用适配器的对象,不需要关心适配器内部的实现,只对接目标角色。 目标角色(Target):目标角色,和client直接对接,定义

    2024年01月18日
    浏览(52)
  • 设计模式 06 适配器模式

    适配器模式(Adapter Pattern)属于 结构型 模式 结构型 模式关注如何将现有的类或对象组织在一起形成更加强大的结构。 在生活中,我们经常遇到这样的一个问题:轻薄笔记本通常只有 type-c 或者 usb-a 接口,没有网口。但日常使用中是往往需要连接网口上网的,这时想到的第

    2024年02月11日
    浏览(29)
  • 设计模式四:适配器模式

    1、适配器模式的理解 适配器模式可以理解为有两个现成的类Adaptee和Target,它们两个是不能动的,要求必须使用B这个类来实现一个功能,但是A的内容是能复用的,这个时候我们需要编写一个转换器 适配器模式 Adaptee:被适配者,现有的接口或者类; Adapter:适配器类,适配器

    2024年02月22日
    浏览(39)
  • 适配器设计模式

    一、适配器模式 B站:java架构师 定义:适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作 三种适配器:类的适配器模式、对象的适配器模式、接口的适配器模式 1.类适配器模式 实现方式:让

    2024年02月11日
    浏览(35)
  • 设计模式——适配器

    说起适配器,大家第一个想到的可能就是电源适配器。 电源适配器的作用想必同学们也都清楚,那就是将220伏高电压转换成想要的5伏至20伏左右稳定的低电压。 从某种程度上讲,编程中经常提起的适配器模式的原理与上面讲到的基本是一致的。 用于将一个类的接口转换成另

    2024年02月12日
    浏览(41)
  • 【设计模式】使用适配器模式做补偿设计

    适配器模式是一种 结构型设计模式 ,它提供了一个中间层,通过这个中间层,客户端可以使用统一的接口与具有不同接口的类进行交互,也就是说,将一个接口转换成客户期望的另一个接口,使得原本不兼容的接口能够协同工作。 举个现实中的例子,我们现在的很多轻薄笔

    2024年02月22日
    浏览(35)
  • js设计模式:适配器模式

    可以将某种不同格式的数据转化为自己所期待的数据格式 或者对于一些存在兼容或者特殊处理的业务逻辑,可以进行一个适配

    2024年02月22日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包