Adapter Pattern: Overview
• Consider the following situation
1. A client is implemented to communicate with class C1 with interface A
2. At some later time, a need arises for a client to communicate with class C2
with interface B, where C1 and C2 are essentially different implementations
of classes that represent the same thing
3. The programmer could change the client’s code, or could change C2’s code,
but we want to avoid modification of existing code
• The purpose of the Adapter Pattern is to allow a class that communicates via
interface A to talk with another that uses interface B
• The adapter converts requests to interface A to ones that are compatible with
those implemented in B
• The advantage to this approach is that it allows 2 systems to coexist and work
together with no change to the original implementations
– All changes are incorporated in the adapter
• The process:
1. Object of class C2 is wrapped by a B adapter
2. Client makes a request of the adapter via the A interface
3. The adapter translates the request into one or more calls to B’s methods
1
Adapter Pattern: Defined
• Adapter Pattern:
Converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because
of incompatible interfaces.
• Class diagram:
• Design principles it incorporates:
1. Composition: Adapter wraps the adaptee
– Can be used with any subclass of the adaptee
2. Binds client to interface, not an implementation
– Can use multiple adapters
– Can add new implementations without modifying code
• Points:
– Size of interface determines complexity of adapter
– Adapter can wrap more than one class
– Adapter can implement more than one interface
2
Adapter Pattern: Example
• Class diagram:
3
Adapter Pattern: Example (2)
• Example code:
public interface Type1Interface {
public ... type1Behavior1(...);
public ... type1Behavior2(...);
...
}
public interface Type2Interface {
public ... type2Behavior1(...);
public ... type2Behavior2(...);
...
}
public class Type2Adapter implements Type1Interface {
Type2Interface obj;
public Type2Adapter (Type2Interface obj) {
this.obj = obj;
}
public ... type1Behavior1 (...) {
obj.type2Behavior1(...);
}
public ... type1Behavior2 (...) {
obj.type2Behavior1(...);
}
...
}
//Subclasses of Type1 and Type2 interfaces as expected // They override behaviors of the interfaces as required
public class Driver {
public static void main (String args[]) {
Type2Interface type2Object = new Type2Subclass1();
Type2Adapter adapter = new Type2Adapter(type2Object);
...
adapter.type1Behavior1(...);
adapter.type1Behavior2(...);
...
}
}
4
Adapter Pattern: Types of Adapters
1. Object Adapter
• Previous discussion dealt with Object Adapter Pattern
• Utilizes composition
• Adapter inherits only from the T arget interface
2. Class Adapter
• Utilizes multiple inheritance
• Class diagram:
5
Adapter Pattern: Issues
• Above discussion assumed a one-to-one correspondence between the methods
of the adapting and adaptee interfaces
1. type1Behavior1 maps to type2Behavor1
2. type1Behavior2 maps to type2Behavor2
3. etc.
• What if a type1 behavior has no counterpart in type2
• In such a case, have the adapter throw an U nsupportedOperationException()
exception
• Assuming that
1. type1Behavior1 maps to type2Behavor1, but
2. type1Behavior2 does NOT map to type2Behavor2
• Example code:
...
public class Type2Adapter implements Type1Interface {
Type2Interface obj;
public Type2Adapter (Type2Interface obj) {
this.obj = obj;
}
public ... type1Behavior1 (...) {
obj.type2Behavior1(...);
}
public ... type1Behavior2 (...) {
throw new UnsupportedOperationException();
}
...
}
...
6
© Copyright 2026 Paperzz