Tuesday, February 10, 2026

PHP Structural Pattern the Adapter with code sample and UML

Adapter Design Pattern

The Adapter Design Pattern, categorized under Structural design patterns, acts as a bridge between two incompatible interfaces, enabling them to work together.

The pattern introduces an intermediary interface (the Adapter) that converts one interface into another, allowing classes with incompatible interfaces to collaborate seamlessly.


Why Should PHP Programmers Study the Adapter Design Pattern?

  1. Legacy System Integration:
    Enables modern PHP systems to work with older codebases without large rewrites.
  2. Third-party Tool Compatibility:
    Smooths integration when external libraries expose incompatible interfaces.
  3. Improved Code Reusability:
    Adapts existing components instead of rewriting working logic.
  4. Better Modularity:
    Keeps interface translation separate from business logic.
  5. Flexible System Evolution:
    Supports adding new functionality without disturbing existing architecture.
  6. Open/Closed Principle:
    Extends behavior without modifying existing classes.
  7. Simplified Interfaces:
    Wraps complex APIs into developer-friendly interfaces.
  8. Versatile Adaptation:
    Multiple adapters can support multiple integration scenarios.

Adapter — UML

Target

  • Defines the interface the Client expects
  • Represents standard system behavior
  • Used directly by the Client

Client

  • Works through the Target interface
  • Is unaware of the Adaptee
  • Relies on Adapter for compatibility

Adaptee

  • Has an incompatible interface
  • Contains useful existing functionality
  • Cannot be easily modified

Adapter

  • Implements the Target interface
  • Translates Client requests
  • Connects incompatible interfaces

Target.php

interface Target
{
    public function request(): string;
}

Adaptee.php

class Adaptee
{
    public function specificRequest(): string
    {
        return "Specific request from Adaptee.";
    }
}

Adapter.php

class Adapter implements Target
{
    private $adaptee;

    public function __construct(Adaptee $adaptee)
    {
        $this->adaptee = $adaptee;
    }

    public function request(): string
    {
        return "Adapter: " . $this->adaptee->specificRequest();
    }
}

index.php

require_once 'Target.php';
require_once 'Adaptee.php';
require_once 'Adapter.php';

function clientCode(Target $target)
{
    echo $target->request();
}

echo "Client code with Adaptee:<br/>";
$adaptee = new Adaptee();
echo $adaptee->specificRequest();
echo "<br/><br/>";

echo "Client code with Adapter:<br/>";
$adapter = new Adapter($adaptee);
clientCode($adapter);

Key Idea

The Adapter pattern wraps an existing class and translates method calls from the Target interface into calls understood by the Adaptee, allowing incompatible interfaces to work together without modification.


S.W.O.T. Analysis — Adapter Pattern (PHP)

Strengths

  • Enables seamless interface compatibility
  • Promotes reuse of legacy and third-party code
  • Decouples client logic from implementation

Weaknesses

  • Adds abstraction layers
  • May introduce slight performance overhead
  • Often temporary when legacy systems are replaced

Opportunities

  • Legacy modernization
  • Third-party API standardization
  • Cross-language integration

Threats

  • Bypassing adapters leads to inconsistency
  • Overuse can clutter architecture
  • Direct refactoring may be a better long-term solution

No comments:

PHP Structural Pattern the Adapter with code sample and UML

Adapter Design Pattern The Adapter Design Pattern , categorized under Structural design patterns, acts as a bridge between two incom...