Design pattern "Adapter"
Real world example
A real-world application of the adapter pattern in this scenario could be seen in a multilingual conference or business meeting where participants speak different languages and require interpretation services to communicate effectively.
Letโs consider three individuals: Alice, who speaks English, Pierre, who speaks French, and Maria, an interpreter fluent in both languages. Hereโs how the adapter pattern can be applied:
-
Define the Common Interface: In this case, the common interface would represent the communication method between participants, which is the ability to speak and understand each other.
-
Create Adapters for Different Languages:
- Alice and Pierre represent the participants who speak different languages and cannot directly communicate with each other.
- Maria acts as an interpreter, who can adapt messages between Alice and Pierre.
-
Utilize the Adapters:
- Alice speaks English, but Pierre speaks French. They cannot understand each other directly.
- Maria, the interpreter, acts as an adapter. She listens to what Alice says in English, interprets it into French for Pierre, and vice versa.
Hereโs how the scenario would play out:
In this scenario:
- Mariaโs role as an interpreter represents the adapter. She adapts the messages between Alice and Pierre, allowing them to communicate effectively despite speaking different languages.
- Alice and Pierre communicate indirectly through Maria, who serves as a bridge between them.
- This adaptation process ensures seamless communication between individuals who speak different languages, demonstrating the adapter patternโs real-world applicability in facilitating interoperability between incompatible interfaces or systems.
In plain words
Adapter pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.
Wikipedia definition
In software engineering, the adapter pattern is a software design 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.
Programmatic example
Consider a notification service application.
First we have an interface Notification
that all types of providers have to implement
And the application expects any implementation of Notification
interface to work.
Now letโs say we have to add a SMSNotification
in our application (from an another package), and we CANNOT modify it.
We canโt implement it directly because SMSNotification
has a different interface. To make it compatible, we will have to create an adapter that is compatible.
And now the SMSNotification
can be used in our application using SMSNotificationAdapter
.
Diagram
Loading graph...
In this example, we CANNOT modify the SMSNotification class. Therefore, we need to create an intermediary class that acts as a connector between our implementation and the SMSNotification implementation.
Some other examples
-
Currency Conversion System: Implement an interface for different currency converters. Create adapters for specific currency conversion APIs to make them compatible.
-
Language Translation Tool: Define an interface for language translation services. Develop adapters for different translation APIs to enable integration.
-
File Format Conversion Tool: Create an interface for file format converters. Implement adapters for converting files between various formats (e.g., PDF to Word).
-
Authentication System: Design a unified authentication interface. Develop adapters for different authentication providers (e.g., Google, Facebook, LDAP).
-
Payment Gateway Integration: Create an interface for processing payments. Develop adapters for different payment gateways (e.g., PayPal, Stripe, Square).
-
Camera Interface for Smartphones: Design a common interface for accessing smartphone cameras. Develop adapters for different camera APIs to ensure compatibility.
-
Remote Control for Smart Devices: Define a common interface for remote control functions. Implement adapters for various smart devices (e.g., smart TVs, air conditioners).
-
Fitness Tracking App: Create an interface for fitness tracking services. Develop adapters for different fitness trackers or wearables.
-
Recipe Management System: Design an interface for recipe storage and retrieval. Implement adapters for various recipe databases or APIs.
Design pattern "Strategy"
Let's learn what is the "Strategy" design pattern
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern ๐ค๐๐ถ๐งณ
Design pattern "Iterator"
Let's learn what is the "Iterator" design pattern ๐
Design pattern "Observer"
Let's learn what is the "Observer" design pattern ๐๐๐๐
Design pattern "Memento"
Let's learn what is the "Memento" design pattern ๐ธ๏ธ๐ผ๏ธ๐ผ๏ธ๐๐ฐ๏ธ๐
Design pattern "Facade"
Let's learn what is the "Facade" design pattern ๐น๏ธ๐๏ธ๐จ
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern ๐ชโโโ
Design pattern "Composite"
Let's learn what is the "Composite" design pattern ๐ณ๐ฟ๐ฟ