Hi,
I've been trying to understand the Adapter
pattern for sometime now. I'm referring to the book 'Head First Design Patterns'. For anybody who has the book, this is on page 251.
A little bit of the background-- I have understood that the adapters basically convert an interface to the type the client expects so incompatible types can work together. Object Adapters do this using composition ( the adapter encapsulates the adaptee and implements the target interface by delegating the target interface method calls to adaptee method calls and/or adding extra processing if required ) and Class Adapters do this using multiple inheritance ( they subclass both the adaptee and the target ). Since
Java does not support multiple inheritance, we will only talk about object adapters.
So given below is a simple Enumeration Iterator that adapts an Enumeration to an Iterator.
So far, so good.
But now on page 251, in the Brain Power section, the authors state the following --
Head First Design Patterns wrote:"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?"
I've been thinking about this question for some time and I don't have an answer. The reason is all adapter implementations would implement a target interface that is already designed. When we code, we code to an interface, not to an implementation. So we can't really invoke methods that aren't a part of target interface.
I think I have probably not understood the question because no matter what pattern we use, the target interface has only four methods, so my adapter can't just add features that aren't provided by the target interface. Or can we?
Could someone please advice.
Thanks.