Adaptor design patterns.
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.
Scenario:
You have an app that expects to print text using a Printer
interface.
But you found an old class called OldPrinter
that doesn't fit your Printer
interface.
You want to adapt it!
1. Target Interface (what the app expects)
2. Adaptee (the old class)
3. Adapter
4. Client Code
✨ Output:
Comments
Post a Comment