The Adapter Pattern is a structural design pattern that allows incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces by wrapping an existing class with a new interface.
In this C++ code, there is a business rule that needs to display the result of the sum 2 + 3, so we create an interface “Adder” for that need, so we can decide later an implementation for it.
Then we have the ArrayAccumulator class to work with, but it doesn’t comply with our interface. We need to adapt its interface for it, so it can be used as an Adder.
We can create the adapter using composition pattern, getting the ArrayAccumulator via dependency injection . then we supply the needed interface with our ArrayAccumulator method.
Finally, we can run the client code, using the ArrayAccumulator as an adder through polymorphism.