This post gives a brief overview about the Adapter Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.
The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.
What is the Adapter Pattern?
The Adapter design pattern solves problems like:
- How can a class be reused that does not have an interface that a client requires?
- How can classes that have incompatible interfaces work together?
- How can an alternative interface be provided for a class?
Often an (already existing) class can’t be reused only because its interface doesn’t conform to the interface clients require.
The Adapter design pattern describes how to solve such problems:
- Define a separate Adapter class that converts the (incompatible) interface of a class (Adaptee) into another interface (Target) clients require.
- Work through an Adapter to work with (reuse) classes that do not have the required interface.
The key idea in this pattern is to work through a separate Adapter that adapts the interface of an (already existing) class without changing it. Clients don’t know whether they work with a Target class directly or through an Adapter with a class that does not have the Target interface.
The following diagram shows the Adapter Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:
@startuml class Client class Target <<interface>> class Adapter class Adaptee Target : request() Adapter : request() Adaptee : specificRequest() Client -> Target Target <|.. Adapter Adapter -> Adaptee note on link Adapter is composed with the Adapter. end note note bottom of Client The client sees only the Target interface end note note "The Adapter implements\nthe Target interface." as n1 Target .. n1 n1 .. Adapter note bottom of Adaptee All requests get delegated to the Adaptee. end note @enduml
Other Design Patterns
In another article you find information about how to put together a single-side web application using PlantUML.
[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]