Answer the question
In order to leave comments, you need to log in
What should be the Adapter pattern?
Help guys urgently, I just can’t figure out this pattern (Adapter), everywhere for example it’s not written clearly (+ wherever I checked, there is no second implementation). Does anyone have a good example of the Adapter pattern, maybe someone tried to implement it for themselves? (the example on cpp-reference is a little off topic, because the problem they solve with an adapter can be solved using inheritance)
Answer the question
In order to leave comments, you need to log in
The adapter adapts the interface.
Let's say there is a TextFormatter class for text formatting, and it has a format() method.
For some reason, the project decided to abandon this class, and use another NewFormatter, like another one that is much better and nicer, but it does not have the format() method, it has the formatText() method.
And in order not to look for a call to the old class throughout the project, by editing a bunch of code, you can write an adapter, replacing the old class with it.
Let it be NewTextFormatterAdapter. In which we write the format() method, in which a new class is called, something like:
return this->newFormatter->formatText();
The adapter is like an adapter from usb to type-c for example. You have a client that works with UsbInterface and some class that does everything the client needs, but has a TypeCInterface. Those. we can't just pass a pointer to it to the client. Of course, you can implement UsbInterface in such a class, but this is not always reasonable and possible. The adapter, in this case, will implement UsbInterface and redirect requests to the target object by calling the appropriate TypeCInterface methods
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question