Design patterns: Adapter

date: · 2 min
author: Mauto
series: Design Patterns/ part 7/24

The classic Gang of Four patterns, one per part, with practical PHP examples.

  1. Design Patterns
  2. Design patterns: Singleton
  3. Design patterns: Factory Method
  4. Design patterns: Abstract Factory
  5. Design patterns: Builder
  6. Design patterns: Prototype
  7. Design patterns: Adapter
  8. Bridge Pattern in PHP: What It Is, When to Use It, with Examples
  9. Design patterns: Composite
  10. Design patterns: Decorator
  11. Design patterns: Facade, What It Is and How to Use It (PHP)
  12. Design patterns: Flyweight
  13. Design patterns: Proxy
  14. Design patterns: Chain of Responsibility Explained (PHP)
  15. Design patterns: Command
  16. Design patterns: Interpreter
  17. Design patterns: Iterator
  18. Design patterns: Mediator
  19. Design patterns: Memento
  20. Design patterns: Observer
  21. Design patterns: State, What It Is and How to Use It (PHP)
  22. Design patterns: Strategy
  23. Design patterns: Template Method
  24. Design patterns: Visitor
the whole guide >

The Adapter pattern is a structural design pattern that allows incompatible interfaces to work together.

Characteristics of the Adapter pattern

  1. Compatibility between interfaces: The Adapter Pattern lets you adapt an existing interface to another interface the client expects, enabling incompatible classes to work together.
  2. Separation of responsibilities: It promotes separation between the client code and the classes that need to be adapted. The client doesn’t have to change its own code to use the adapted classes.
  3. Reuse of existing code: It lets you reuse existing classes without modifying them, making it easier to integrate new functionality into an application.
  4. Flexibility: Adapters can easily be created to adapt new classes, keeping the application flexible and making it easier to integrate different components.
  5. Translating calls: It translates the client’s calls into the format expected by the adapted class.
  6. Ability to adapt different kinds of classes: It can adapt both classes and interfaces, letting you work with various kinds of components, including external objects or third-party libraries.
  7. Incremental development: It supports an incremental development approach, letting you add new features or components without having to restructure the entire system.

Example in PHP:

class OldPaymentSystem {
    public function pay() {
        return "Payment with the old system.";
    }
}

interface NewPaymentInterface {
    public function processPayment();
}

class PaymentAdapter implements NewPaymentInterface {
    private $oldSystem;

    public function __construct(OldPaymentSystem $oldSystem) {
        $this->oldSystem = $oldSystem;
    }

    public function processPayment() {
        return $this->oldSystem->pay(); // We adapt the old method to the new system.
    }
}

// Usage
$oldPayment = new OldPaymentSystem();
$adapter = new PaymentAdapter($oldPayment);
echo $adapter->processPayment();

BACK TO THE GUIDE

Copied