Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a two-way adapter pattern

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 ??

like image 894
Priyanka Avatar asked Oct 14 '11 02:10

Priyanka


People also ask

What are the two versions of Adapter pattern?

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.

What does the Adapter pattern do?

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.

What is an adapter design pattern explain with example?

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.

What is Adapter pattern give real life example?

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.


2 Answers

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.

Adapter Design Pattern

Source: http://www.oodesign.com/adapter-pattern.html

like image 56
user48903 Avatar answered Dec 13 '22 15:12

user48903


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.

like image 37
Vincent Ramdhanie Avatar answered Dec 13 '22 17:12

Vincent Ramdhanie