Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the Adapter pattern of C++ Design pattern

Shulou Source: shulou.com Published: 2022-05-31 22:12:10 09月13日 Update

This article mainly introduces "how to implement the adapter pattern of C++ design pattern". In daily operation, I believe many people have doubts about how to implement the adapter pattern of C++ design pattern. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize the adapter pattern of C++ design pattern". Next, please follow the editor to study!

As the name implies, the adapter pattern lies in the conversion of the interface. The most vivid example is the two-port to three-port power adapter. The number of ports can be understood as the number of parameters, just as we call the interface with three parameters, and the interface provided has only two parameters. It takes a long time for the adapter class to expand and transform the interface, which is the main significance of the existence of the adapter pattern.

Action

Convert the interface of one class to another interface that the customer wants, so that those classes that can not work together because of the incompatibility of the interface can work together, so that an original object outside the control range can match an interface. The adapter pattern is mainly used in situations where you want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment.

You want to use an existing class, but if its interface, that is, its method is different from your requirements, you should consider using the adapter pattern.

Class view

Class pattern adapter

Object pattern adapter

Code implementation

/ / Target interface class. The interface class Target {public: Target () {} virtual ~ Target () {} virtual void Request () / / defines a new standard interface {cout

Tags: Interfaces patterns adapters adapters objects design patterns effects learning parameters subclasses reuse important two can pass customers that is come on quantity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Xiaomi MariaDB Apple