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?

According to Wikipedia, the Adapter Pattern is a design patternthat allows the interface of an existing class to be used as another interface.

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.

UML Diagram

The following diagram shows the Adapter Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:</p><p>@startuml<br /> class Client<br /> class Target <<interface>><br /> class Adapter<br /> class Adaptee<br /> Target : request()<br /> Adapter : request()<br /> Adaptee : specificRequest()</p><p>Client -> Target<br /> Target <|.. Adapter<br /> Adapter -> Adaptee<br /> note on link<br /> Adapter is composed<br /> with the Adapter.<br /> end note</p><p>note bottom of Client<br /> The client sees only the<br /> Target interface<br /> end note</p><p>note “The Adapter implements\nthe Target interface.” as n1<br /> Target .. n1<br /> n1 .. Adapter</p><p>note bottom of Adaptee<br /> All requests get<br /> delegated to the<br /> Adaptee.<br /> end note<br /> @enduml</p><p>

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:


@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”]