Adapter Pattern
In Object-Oriented Programming (OOP), the Adapter Pattern is a structural design pattern that allows objects with incompatible interfaces to work together. It's often used to make existing classes work with others without modifying their source code.
The Adapter Pattern involves the introduction of an extra layer of abstraction which translates, or adapts, the interface of one class (the adaptee) into an interface that the client class can use.
In other words, the Adapter Pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.
The Adapter pattern involves the following components:
Target: This is the interface that the client class interacts with. It defines the domain-specific interface used by the client code.
Client: This is the class that interacts with a service that it cannot use directly. The Client collaborates with objects that implement the Target interface.
Adaptee: This is the class that the client wants to use. It contains some useful behavior, but its interface is incompatible with the client's interface. The Adaptee needs some adaptation before the client can use it.
Adapter: This class makes the Adaptee's interface compatible with the Target's interface. It wraps the Adaptee and translates the interface of the Adaptee into an interface that the Target can use.
The Adapter Pattern is very useful when you want to use a library or a class that does something similar to what you need, but its interface isn't compatible with the rest of your code. The Adapter Pattern lets you create a middle-layer class that serves as a translator between your code and the legacy or third-party class. This pattern is also helpful when you want to refactor or modernize a legacy codebase, where changing the existing interfaces directly isn't a feasible option.