KEMBAR78
Adapter Design Pattern | PDF | Interface (Computing) | Computer Science
0% found this document useful (0 votes)
5 views4 pages

Adapter Design Pattern

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Adapter Design Pattern

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Two incompatible interfaces or systems can cooperate by using the adapter design pattern, a structural

design pattern. Because of incompatible interfaces, it serves as a bridge between two classes that
would not otherwise be able to communicate. The adapter approach is very helpful when attempting
to incorporate third-party libraries or legacy code into a new system.

Real-World Example of Adapter Design Pattern

Let's understand this concept using a simple example:

Suppose you have two buddies, one of them speaks French exclusively and the other English
exclusively. The language barrier prevents them from communicating the way you want them to.
• You act as an adapter, translating messages between them. Your role allows the English
speaker to convey messages to you, and you convert those messages into French for the other
person.

• In this way, despite the language difference, your adaptation enables smooth communication
between your friends.

• This role you play is similar to the Adapter design pattern, bridging the gap between
incompatible interfaces.

Components of Adapter Design Pattern

Below are the components of adapter design pattern:

• Target Interface: Defines the interface expected by the client. It represents the set of
operations that the client code can use. It's the common interface that the client code interacts
with.

• Adaptee: The existing class or system with an incompatible interface that needs to be
integrated into the new system. It's the class or system that the client code cannot directly use
due to interface mismatches.

• Adapter: A class that implements the target interface and internally uses an instance of the
adaptee to make it compatible with the target interface. It acts as a bridge, adapting the
interface of the adaptee to match the target interface.

• Client: The code that uses the target interface to interact with objects. It remains unaware of
the specific implementation details of the adaptee and the adapter. It's the code that benefits
from the integration of the adaptee into the system through the adapter.

How Adapter Design Pattern works?

Below is how adapter design pattern works:

• Step 1: The client initiates a request by calling a method on the adapter via the target interface.

• Step 2: The adapter maps or transforms the client's request into a format that the adaptee can
understand using the adaptee's interface.

• Step 3: The adaptee does the actual job based on the translated request from the adapter.

• Step 4: The client receives the results of the call, remaining unaware of the adapter's presence
or the specific details of the adaptee.
• Problem Statement:

Let's consider a scenario where we have an existing system that uses a


LegacyPrinter class with a method named printDocument() which we want to
adapt into a new system that expects a Printer interface with a method
named print(). We'll use the Adapter design pattern to make these two
interfaces compatible.

// Adapter Design Pattern Example Code in Java

// Target Interface

interface Printer {

void print();

// Adaptee

class LegacyPrinter {

public void printDocument() {

System.out.println("Legacy Printer is printing a document.");


}

// Adapter

class PrinterAdapter implements Printer {

private LegacyPrinter legacyPrinter;

public PrinterAdapter() {

this.legacyPrinter = new LegacyPrinter();

@Override

public void print() {

legacyPrinter.printDocument();

// Client Code

public class AdapterPatternDemo {

public static void clientCode(Printer printer) {

printer.print();

public static void main(String[] args) {

// Using the Adapter

PrinterAdapter adapter = new PrinterAdapter();

clientCode(adapter);

}
Pros of Adapter Design Pattern

Below are the pros of Adapter Design Pattern:

• By creating an adapter, you can reuse existing code without needing to modify it. This
promotes code reuse and helps maintain a cleaner architecture.

• By separating the issues of interface adaptation, the adapter pattern frees classes to
concentrate on their main duties without having to deal with adaptation code that clogs their
logic.

• Because you can simply switch out multiple adapters to support different interfaces without
altering the underlying system.

• By separating your system from particular implementations, adapters make it simpler to swap
out or modify parts without compromising the functionality of other parts.

You might also like