Applied Design Patterns with Java
Structural :: Adapter (137) {C ch 9}
Applicability
Use the Adapter pattern when:
- an existing class does not have an interface matching
a current need;
- there is a need to create a reusable class that cooperates
with unrelated or unforeseen classes (without compatible interfaces);
- (object Adapter only) there are several existing subclasses, but it's impractical
to adapt their interface by subclassing them. An object Adapter can adapt the interface of its parent class.
Structure
A class adapter uses multiple inheritance to adapt one
interface to another:
An object adapter relies on object composition:
Participants
- Target
(Shape) defines the domain-specific interface that Client uses.
- Client
(DrawingEditor) collaborates with objects conforming to the Target interface.
- Adaptee
(TextView) defines an existing interface that needs adapting.
- Adapter
(TextShape) adapts the interface of Adaptee to the Target interface.
Catalog Structural Prev Next