Adapter Design Pattern

Bridge between incompatible interfaces

In a nutshell, the adapter Design Pattern is a powerful tool for building flexible and future-proof software by creating bridges between incompatible interfaces.

Flow

  1. Client wants “A”, adaptee provides “B”.
  2. Adapter wraps “B” and pretends to be “A”.
  3. Client interacts with adapter, unaware of “B”.

Benefits

Example

# Target interface: the interface the client expects
class Target:
    def request(self):
        pass

# Adaptee: the existing class with a different interface
class Adaptee:
    def specific_request(self):
        print("Adaptee's specific request")

# Adapter: bridges the gap between Target and Adaptee
class Adapter(Target):
    def __init__(self, adaptee):
        self.adaptee = adaptee

    def request(self):
        self.adaptee.specific_request()

# Client code
client = Client()
adapter = Adapter(Adaptee())
client.do_something(adapter)  # Client interacts with the adapter seamlessly

Remember