What is Adapter pattern?
💻 coding

What is Adapter pattern?

1 min read 276 words
1 min read
ShareWhatsAppPost on X
  • 1The adapter pattern allows existing classes to be used with different interfaces without modifying their source code.
  • 2It is part of the twenty-three GoF design patterns aimed at solving recurring design problems in software engineering.
  • 3The pattern defines a separate Adapter class that converts incompatible interfaces, enabling classes to work together seamlessly.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The adapter pattern allows existing classes to be used with different interfaces without modifying their source code."

What is Adapter pattern?

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.

The Adapter design pattern is one of the twenty-three well-known GoF 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'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.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 1 August 2018 · 1 min read · 276 words

Part of AskGif Blog · coding

You might also like