0% found this document useful (0 votes)
27 views4 pages

Adapter Design Pattern Explained

The Adapter Design Pattern, also known as the Wrapper, allows two classes to work together that otherwise would have incompatible interfaces. In this case, this software design pattern maps particularly well to the real-world example of an electrical power adapter, which must be used to allow a device to use power.

Uploaded by

arsham
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)
27 views4 pages

Adapter Design Pattern Explained

The Adapter Design Pattern, also known as the Wrapper, allows two classes to work together that otherwise would have incompatible interfaces. In this case, this software design pattern maps particularly well to the real-world example of an electrical power adapter, which must be used to allow a device to use power.

Uploaded by

arsham
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

▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube!

Check it out » Hide

Adapter Design Pattern


INTRODUCTION O N T H I S PA G E

🔍 SEARCH Intent

References
▲ D E S I G N PAT T E R N S

Overview

Abstract Factory

Adapter

Builder

Chain of Responsibility

CQRS

Decorator
Better Software, Faster
Domain Events Pattern
NimblePros helps development teams deliver better
software, faster. Find out how.
Facade
Sponsored
Factory Method

Guard Clause
The Adapter Design Pattern, also known as the Wrapper, allows two classes to work together that
Mediator otherwise would have incompatible interfaces. In this case, this software design pattern maps
particularly well to the real-world example of an electrical power adapter, which must be used to
Memento allow a device to use power. For instance, most mobile devices today can be powered via some
form of USB power, or via AC current. However, in both cases there is no direct way for the
Null Object
device to plug into the wall or USB port. The solution is an adapter. In fact, in some cases
Object Mother
Observer

adapters
▶️ Want to learn more about design can be Learn
patterns? chained together,
more as in the case
from NimblePros on of a USB-to-Device
YouTube! Check it cable
out » that can also be
Hide
plugged into a wall or car electrical outlet that has a USB power port.

The Adapter pattern relies on a common abstraction which defines the interface client code will
consume. Different implementations of this interface are then created to support different
otherwise incompatible ways of achieving the goal of the abstraction. For example, an application
may wish to send notifications as part of its functionality. However, there are several approaches
to sending notifications that have different interfaces:

SendEmail(string toEmail, string fromEmail, string subject, string body)

SendText(string toNumber, string message)


SendToastNotification(string username, string message)

Instead of writing complex code that needs to work with all of these different interfaces, an
adapter interface can be used:

Copy

public interface INotificationAdapter


{
void Notify(User user, Message message);
}

Given this adapter interface, specific implementations can be written to support each messaging
library:

Copy

public class EmailNotificationAdapter : INotificationAdapter


{
private readonly IEmailSender _emailSender;
private readonly EmailSettings _settings;

public EmailNotificationAdapter(IEmailSender emailSender,


EmailSettings settings)
{
_emailSender = emailSender;
_settings = settings;

}
▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube! Check it out » Hide

public void Notify(User user, Message message)


{
if(![Link]) return;

string fromEmail = _settings.DefaultFromAddress;

_emailSender.Send([Link], fromEmail, [Link], [Link]);


}
}

Notice that the INotificationAdapter abstraction does not expose any details about how the
notification might be sent. An Adapter should ideally be written using the abstractions of the
application that will be using it, and should avoid exposing implementation details implicitly or
explicitly. Recall that the Dependency Inversion Principle requires that abstractions should not
depend on details.

The Adapter pattern also enables the Open-Closed Principle, since new functionality can be
added to the system's use of notifications without requiring existing code to be modified.
Instead, new instances of the INotificationAdapter can be added, extending the ways in which
notifications may be sent.

Adapters are frequently used in Domain-Driven Design as part of Anti-Corruption Layers.

Intent
Convert the interface of a class into another interface clients expect. Adapter lets classes work
together that couldn't otherwise because of incompatible interfaces. GoF

References
Pluralsight - C# Design Patterns: Adapter

Amazon - Design Patterns: Elements of Reusable Object-Oriented Software - Gang of Four

Pluralsight - Design Patterns Library


Edit this page on GitHub
▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube! Check it out » Hide

PREV NEXT
Abstract Factory Builder

You might also like