In the book Design Patterns by GoF (Eric Gamma and others), two-way adapter pattern is mentioned which is used for transparency. They are useful when 2 different clients want to view the same object differently. Could some one give a C++ example for the same and in which practical scenarios it can be used ??
If you do some research on the adapter pattern, you will find two different versions of it: The class adapter pattern that implements the adapter using inheritance. The object adapter pattern that uses composition to reference an instance of the wrapped class within the adapter.
The adapter pattern convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. The client sees only the target interface and not the adapter. The adapter implements the target interface.
Professional Logo DesignThis pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. A real life example could be a case of card reader which acts as an adapter between memory card and a laptop.
One of the great real life example of Adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.
The Two-Ways Adapters are adapters that implements both interfaces of Target and Adaptee. The adapted object can be used as Target in new systems dealing with Target classes or as Adaptee in other systems dealing with Adaptee classes. Going further on this line of thinking, we can have adapters implementing n interfaces, adapting to n systems. Two-way adapters and n-way adapters are hard to implement in systems not supporting multiple inheritance. If adapter has to extend the Target class it can not extent another class like Adaptee, so the Adaptee should be an interface and all the calls should be delegated from the adapter to the Adaptee object.
Source: http://www.oodesign.com/adapter-pattern.html
I find that the explanation given here is quite good. It is a C# example but the explanation is clear and the example can quite easily be translated into C++ code. The example is also quite detailed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With