介紹
? ? ? ? 適配器模式將一個(gè)接口轉(zhuǎn)換成客戶希望的另一個(gè)接口,使接口不兼容的那些類(lèi)可以一起工作,其別名為包裝器。
實(shí)現(xiàn)
對(duì)象適配器模式
myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
class Target { // 目標(biāo)類(lèi)
public:
virtual void request() = 0;
};
class Adaptee { // 適配者類(lèi)
public:
void specificRequest();
};
class Adapter : public Target { // 適配器類(lèi)
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;
}
類(lèi)適配器模式
myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
class Target { // 目標(biāo)類(lèi)
public:
virtual void request() = 0;
};
class Adaptee { // 適配者類(lèi)
public:
void specificRequest();
};
class Adapter : public Target, public Adaptee { // 適配器類(lèi)
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 { // 目標(biāo)類(lèi)
public:
void request();
};
class Adaptee { // 適配者類(lèi)
public:
void specificRequest();
};
class Adapter { // 適配器類(lèi)
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 { // 缺省適配器類(lèi)
public:
virtual void request() override;
virtual void request2() override;
virtual void request3() override;
};
class Adaptee { // 適配者類(lèi)
public:
void specificRequest();
};
class Adapter : public AbstractService { // 具體適配器類(lèi)
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 << "缺省函數(shù)request" << std::endl;
}
void AbstractService::request2() {
std::cout << "缺省函數(shù)request2" << std::endl;
}
void AbstractService::request3() {
std::cout << "缺省函數(shù)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;
}
總結(jié)
優(yōu)點(diǎn)
? ? ? ? 1.?將目標(biāo)類(lèi)和適配者類(lèi)解耦。通過(guò)引入一個(gè)適配器類(lèi)來(lái)重用現(xiàn)有的適配者類(lèi),無(wú)須修改原有結(jié)構(gòu)。
? ? ? ? 2.?增加了類(lèi)的透明性和復(fù)用性。將具體的業(yè)務(wù)實(shí)現(xiàn)過(guò)程封裝在適配者類(lèi)中,對(duì)于客戶端類(lèi)而言是透明的,而且提高了適配者類(lèi)的復(fù)用性,同一個(gè)適配者類(lèi)可以在多個(gè)不同的系統(tǒng)中復(fù)用。
? ? ? ? 3.?靈活性和擴(kuò)展性都非常好。通過(guò)使用配置文件,可以很方便地更換適配器,也可以在不修改原有代碼的基礎(chǔ)上增加新的適配器類(lèi),完全符合開(kāi)閉原則。
缺點(diǎn)
? ? ? ? 1.?對(duì)于Java、C#等不支持多重類(lèi)繼承的語(yǔ)言,一次最多只能適配一個(gè)適配者類(lèi),不能同時(shí)適配多個(gè)適配者。
? ? ? ? 2.?適配者類(lèi)不能為最終類(lèi),例如在Java中不能為final類(lèi),C#中不能為sealed類(lèi)。
? ? ? ? 3.?在Java、C#等語(yǔ)言中,類(lèi)適配器模式中的目標(biāo)抽象類(lèi)只能為接口,不能為類(lèi),其使用有一定的局限性。
適用場(chǎng)景
? ? ? ? 1.?系統(tǒng)需要使用一些現(xiàn)有的類(lèi),而這些類(lèi)的接口(例如方法名)不符合系統(tǒng)的需要,甚至沒(méi)有這些類(lèi)的源代碼。
? ? ? ? 2.?想創(chuàng)建一個(gè)可以重復(fù)使用的類(lèi),用于與一些彼此之間沒(méi)有太大關(guān)聯(lián)的類(lèi),包括一些可能在將來(lái)引進(jìn)的類(lèi)一起工作。
練習(xí)
對(duì)象適配器模式
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
class Encryption { // 目標(biāo)類(lèi)
public:
virtual std::string encrypt(std::string data) = 0;
};
class ThirdLib { // 適配者類(lèi)
public:
std::string specificEncrypt(std::string data);
std::string specificDecrypt(std::string data);
};
class AdapterEncyption : public Encryption { // 適配器類(lèi)
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;
}
類(lèi)適配器模式
myclass.h
//
// Created by yuwp on 2024/1/12.
//
#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H
#include <iostream>
class Encryption { // 目標(biāo)類(lèi)
public:
virtual std::string encrypt(std::string data) = 0;
};
class ThirdLib { // 適配者類(lèi)
public:
std::string specificEncrypt(std::string data);
std::string specificDecrypt(std::string data);
};
class AdapterEncyption : public Encryption, public ThirdLib { // 適配器類(lèi)
public:
AdapterEncyption();
~AdapterEncyption();
std::string encrypt(std::string data) override;
};
#endif //DESIGNPATTERNS_MYCLASS_H
myclass.cpp文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-801836.html
//
// 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文章來(lái)源地址http://www.zghlxwxcb.cn/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;
}
到了這里,關(guān)于《設(shè)計(jì)模式的藝術(shù)》筆記 - 適配器模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!