Adapter Pattern
Adapter Pattern
AC Power Adapters
Electronic products made for the USA cannot be used directly with electrical outlets found in
most other parts of the world. US 3-prong (grounded) plugs are not compatible with European
wall outlets.
To use, you need either
• An AC power adapter, if the US product has a “universal” power supply, or
• An AC power converter/adapter, if it doesn’t
Type of Adapter:
Object Adapters
Object Adapters use a compositional technique to adapt one interface to another. The adapter
inherits the target interface that the client expects to see, while it holds an instance of adaptee.
Object adapters enable the client and the adaptee to be completely decoupled from each other.
Only the adapter knows about both of them.
Class Adapters
Class adapters use multiple inheritance to achieve their goals. As in the object adapter, the class
adapter inherits the interface of the client’s target. However, it also inherits the interface of the
adaptee as well. Since Java does not support true multiple inheritance, this means that one of the
interfaces must be inherited from a Java Interface type. Note that either or both of the target or
adaptee interfaces could be a Java Interfaces. The request to the target is simply rerouted to the
specific request that was inherited for the adaptee interface.
Object Adapters Vs. Class Adapters
As Object Adapter uses composition it can not only adapt an adaptee class, but any of its
subclasses. It is flexible.
Class Adapter is committed to only one adaptee. But again it has an advantage as no need to
implement the entire adaptee. It can just override the behaviour of adaptee and also can
override the behavior as it is sub classing.
Class adapters are simpler than object adapters in that they involve fewer classes and are
useful if total decoupling of the client and adaptee is not needed.
Note that class adapters have a problem with name conflicts if methods of the same signature
exist on both the target and the adaptee. Note that just because two objects have methods that
have the same signature (syntax), it does not guarantee that the two methods have the same
meaning or behavior (semantics). That is, the two methods do not necessarily map directly to
each other. Object adapters do not have this problem.
Case Study
Client only understands the SquarePeg interface for inserting pegs using the insert() method. At
back-end application should reuse insert logic within round pegs?
Solution
One Way Adaptor (Object Adaptor)
Two Way Adaptor (Class Adaptor)
Class Diagram:
Adapter Pattern
Explanation:
1. The client makes a request on the adapter by invoking a method from the target interface on it
2. The adapter translates that request into one or more calls on the adaptee using the adaptee
interface
3. The client receives the results of the call and never knows there is an adapter doing the
translation
Code:
Where To Use
We can not change the library interface, since we may not have its source code. Even if we did
have the source code, we probably should not change the library for each domain-specific
application.
Sometimes a toolkit or class library can not be used because its interface is incompatible with the
interface required by an application.
Want to create a reusable class that cooperates with unrelated classes with incompatible
interfaces.
Implementation Issues
How much adapting should be done?
Simple interface conversion that just changes operation names and order of arguments
Totally different set of operations
Does the adapter provide two-way transparency?
A two-way adapter supports both the Target and the Adaptee interface. It allows an adapted
object (Adapter) to appear as an Adaptee object or a Target object.