ArticleslgStudy

computer science

Adapter pattern

Adapter pattern is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Adapter pattern rather than just read about it. In short: In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.

Adapter pattern — main illustration
Adapter pattern — illustration

Key takeaways

  • Adapter pattern belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Adapter pattern to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Adapter pattern from memory before moving on to harder problems.

Reference excerpt

In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code. An example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed.

Overview The adapter design pattern is one of the twenty-three well-known Gang of Four design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. 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 not be reused only because its interface does not 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. See also the UML class diagram below.

Definition An adapter allows two incompatible interfaces to work together. This is the real-world definition for an adapter. Interfaces may be incompatible, but the inner functionality should suit the need. The adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

Usage An adapter can be used when the wrapper must respect a particular interface and must support polymorphic behavior. Alternatively, a decorator pattern makes it possible to add or alter behavior of an interface at run-time, and a facade pattern is used when an easier or simpler interface to an underlying object is desired.

Structure

UML class diagram

In the above UML class diagram, the client class that requires a target interface cannot reuse the adaptee class directly because its interface doesn't conform to the target interface. Instead, the client works through an adapter class that implements the target interface in terms of adaptee:

The object adapter way implements the target interface by delegating to an adaptee object at run-time (adaptee.specificOperation()). The class adapter way implements the target interface by inheriting from an adaptee class at compile-time (specificOperation()).

Object adapter pattern In this adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.

Class adapter pattern This adapter pattern uses multiple polymorphic interfaces implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java (before JDK 1.8) that do not support multiple inheritance of classes.

Implementation of the adapter pattern When implementing the adapter pattern, for clarity, one can apply the class name [ClassName]To[Interface]Adapter to the provider implementation; for example, DAOToProviderAdapter. It should have a constructor method with an adaptee class variable as a parameter. This parameter will be passed to an instance member of [ClassName]To[Interface]Adapter. When the client method is called, it will have access to the adaptee instance that allows for accessing the required data of the adaptee and performing operations on that data that generates the desired output.

See also Adapter Java Design Patterns - Adapter Delegation, strongly relevant to the object adapter pattern. Dependency inversion principle, which can be thought of as applying the adapter pattern, when the high-level class defines its own (adapter) interface to the low-level module (implemented by an adaptee class). Ports and adapters architecture Shim Wrapper function Wrapper library

References

Illustrations

Adapter pattern: The object adapter pattern expressed in UML
The object adapter pattern expressed in UML
Adapter pattern: The object adapter pattern expressed in LePUS3
The object adapter pattern expressed in LePUS3
Adapter pattern: The class adapter pattern expressed in UML
The class adapter pattern expressed in UML
Adapter pattern: The class adapter pattern expressed in LePUS3
The class adapter pattern expressed in LePUS3

Worked examples

Example 1 — a first encounter with Adapter pattern

Start with the simplest possible case. Write down what Adapter pattern claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Adapter pattern before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Adapter pattern ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Adapter pattern

In research
Adapter pattern appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Adapter pattern in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Adapter pattern is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Adapter pattern outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Adapter pattern” →

How to study Adapter pattern in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Adapter pattern means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Adapter pattern out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Adapter pattern in simple terms?

In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others witho…

Why does Adapter pattern matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Adapter pattern?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Adapter pattern.

Tags

  • Software design patterns

Keep exploring