Adapter
Adapter
Adapters
Real world is full of them!
Some examples?
The adapter implements the interface your classes Your Existing expect System
What to do? Write a class that adapts the new vendor interface into the one youre expecting. And talks to the vendor interface to service
your requests Adapter Vendor Class
==
No code changes
Now.
Lets say you are short on Duck objects and would like to use some Turkey objects in their place. Cant use them outright because they have a different interface.
public class TurkeyAdapter implements Duck { Turkey turkey; public TurkeyAdapter (Turkey turkey) { this.turkey = turkey; } Next, we need to get a reference to the object that we public void quack ( ) { are adapting; here we do that through the constructor. turkey.gobble ( ); } public void fly ( ){ Now we need to implement all the methods in the for (int j = 0; j<5; j++) interface; the quack() translation between classes is easy; turkey.fly ( ); just call the gobble method. } } Even though both interfaces have a fly ( ) method, Turkeys fly in } short spurts -- they cant do long distance flying like ducks. To map between a Ducks fly ( ) method and a Turkeys we need to call the Turkeys fly ( ) method five times to make up for it. First, you need to implement the interface of the type you are adapting to. This is the interface your client expects.
Client
The client sees only the Target interface.
Full of good OO design principles: --Use of object composition --Pattern binds the client to an interface and not an implementation
Adapter request ( )
Adaptee specificRequest ()
Difference: The only difference is that with class adapter we subclass the Target and the Adaptee, while the object adapter uses composition to pass requests to an adaptee.
Adapter request ( )
Question: Object Adapters and Class Adapters use two different means of adapting the adaptee (composition versus inheritance). How do these implementations affect the flexibility of the adapter?
Enumeration has a simple interface. Tells whether there are any more elements in the collection. Returns the next element Tells you if you have looked at all the elements Gets the next one
And todaylegacy code that exposes the Enumerator interface. Yet we want new code to use Iterators. Need an adapter.
But what about this method remove ( ) in Iterator? Theres nothing like that in Enumeration.
We are making the Enumerations in your old code look like Iterators for your new code.
Here the adapter is not perfect but is a reasonable solution as long as the client is careful and the adapter is well-documented.
Question: Some AC adapters do more than just change the interface -- they add other features like surge protection, indicator lights, and other bells and whistles. If you were going to implement these kinds of features, what pattern would you use?
Summary
When you need to use an existing class and its interface is not the one you need, use an adapter. An adapter changes an interface into one a client expects. Implementing an adapter may require little work or a great deal of work depending on the size and complexity of the target interface. There are two forms of adapter patterns: object and class adapters. Class adapters require multiple inheritance. An adapter wraps an object to change its interface, a decorator wraps an object to add new behaviors and responsibilities.