| Old Zhou talks about coding, the flowers fall under the keyboard; each line weaves a dream across the galaxy, poetry and coding nurture a clear virtue. |
I am Old Zhou! Follow the “Old Zhou Talks Code” public account for more selected content!┉ ∞ Concept ∞ ┉
| Basic Concept: The adapter pattern converts the interface of a class into another interface that clients expect, allowing classes that cannot work together due to incompatible interfaces to work together.In simpler terms: Old Zhou once went on a business trip to Turkey, where the domestic voltage is 220V, while Turkey’s voltage is 380V. This would prevent Old Zhou’s phone and laptop from charging. What to do? At this time, there is a device called a “plug adapter” that can convert 380V voltage to 220V voltage. We refer to this process, which allows two incompatible objects to cooperate, as the adapter pattern. |
┉ ∞ Implementation of the Adapter Pattern ∞ ┉
The principle: Old Zhou’s device (CChineseDevice) plugs into the “plug adapter” (CVoltageAdapter), which connects to the Turkish socket (CTurkishSocket) providing 380V voltage (supply380vPower()), converting (supplyPower()) to achieve the effect of 220V voltage for the Chinese socket (CChineseSocket) (supply220vPower). As follows:![]() |
|
// adapter.cpp #include <iostream> #include <memory> using namespace std; class CChineseSocket{ // Chinese socket 220VChineseDevice public: virtual ~CChineseSocket() = default; virtual void supplyPower() const{ cout<<“voltage: 220v “<<endl; } }; class CTurkishSocket{ // Turkish socket 380V public: void supply380vSocket() const{ cout<<“voltage: 380v”<<endl; } }; class CVoltageAdapter : public CChineseSocket{ // Adapter public: CVoltageAdapter():m_pTurkishSocket(make_unique<CTurkishSocket>()){} public: void supplyPower() const{ cout<<“begin 380v -> 220v”<<endl; m_pTurkishSocket->supply380vSocket(); // todo: conversion action … cout<<“end 380v -> 220v”<<endl; } private: unique_ptr<CTurkishSocket> m_pTurkishSocket; }; class CChineseDevice{ // Chinese device public: void charge(const CChineseSocket& socket) const{ cout<<“Chinese device prepare to charge …”<<endl; socket.supplyPower(); cout<<“charging …”<<endl; } }; int main(){ cout<<“laoz to turkey”<<endl; CChineseDevice laptop; // Laptop cout<<“Use adapter charge:”<<endl; CVoltageAdapter adapter; // Adapter laptop.charge(adapter); // Laptop plugs into adapter cout<<“laoz’s laptop is charging now”<<endl; return 0; } |
| Execution Result:# g++ adapter.cpp -o adapter# ./adapter
laoz to turkey Use adapter charge: Chinese device prepare to charge … begin 380v -> 220v voltage: 380v end 380v -> 220v charging … laoz’s laptop is charging now |
┉ ∞ Application Scenarios in Real Projects∞ ┉
| Application Scenario One: Before Old Zhou’s self-developed algorithm matured, he interfaced with multiple third-party vehicle algorithms (ADAS, DSM, BSD algorithms), but the standard was based on his self-developed algorithm, creating an adapter for each algorithm to adapt.Application Scenario Two: Those who have developed for Android should know that there is a BaseAdapter class used to provide data refresh for ListView, GridView, RecyclerView, etc. This is a typical use of the adapter pattern (though this is not C++, it is very authoritative). |
Old Zhou: “True friendship is like an adapter: no matter how the interface changes, it can always be compatible with your true self, allowing the communication of souls to never be interrupted.“
