Incredible 06 Adapter Pattern in Java

Incredible 06 Adapter Pattern in Java

Adapter Pattern in Java: Making Incompatible Interfaces Work Together

The Problem

You have an existing class with a useful implementation, but its interface doesn’t match what your code expects — often a third-party library or legacy system. The Adapter pattern converts the interface of a class into another interface clients expect.

Diagram

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor': '#eef2ff', 'primaryTextColor': '#1e293b', 'primaryBorderColor': '#818cf8', 'lineColor': '#94a3b8', 'secondaryColor': '#f0fdf4', 'secondaryBorderColor': '#86efac', 'tertiaryColor': '#fff7ed', 'tertiaryBorderColor': '#fdba74', 'background': '#ffffff', 'mainBkg': '#eef2ff', 'secondBkg': '#f0fdf4', 'tertiaryBkg': '#fff7ed', 'noteBkgColor': '#fff7ed', 'noteBorderColor': '#fdba74', 'actorBkg': '#eef2ff', 'actorBorder': '#818cf8' }}}%%
classDiagram
    class PaymentGateway {
        <<interface>>
        +charge(String customerId, double amount)
    }
    class LegacyBillingSystem {
        +processPayment(String account, long cents)
    }
    class LegacyBillingAdapter {
        -LegacyBillingSystem legacySystem
        +charge(String customerId, double amount)
    }

    PaymentGateway <|.. LegacyBillingAdapter
    LegacyBillingAdapter --> LegacyBillingSystem : adapts

Java Example

// Target interface your application expects
public interface PaymentGateway {
    void charge(String customerId, double amount);
}

// Existing incompatible class (e.g., legacy library)
public class LegacyBillingSystem {
    public void processPayment(String account, long amountInCents) {
        System.out.println("Legacy charge: " + account + " -> " + amountInCents + " cents");
    }
}

// Adapter bridges the two
public class LegacyBillingAdapter implements PaymentGateway {

    private final LegacyBillingSystem legacySystem;

    public LegacyBillingAdapter(LegacyBillingSystem legacySystem) {
        this.legacySystem = legacySystem;
    }

    @Override
    public void charge(String customerId, double amount) {
        long cents = Math.round(amount * 100);
        legacySystem.processPayment(customerId, cents);
    }
}

Quarkus Example

Adapters are everywhere when integrating Quarkus services with legacy BSS/OSS systems that expose SOAP, fixed-width files, or non-TMF interfaces. You wrap the legacy client behind a TMF-shaped interface:

@ApplicationScoped
public class LegacyCrmPartyAdapter implements PartyManagement {

    @Inject
    @RestClient
    LegacySoapCrmClient legacyClient;

    @Override
    public Party getParty(String id) {
        LegacyCustomerRecord record = legacyClient.fetchCustomer(id);
        return new Party(record.getCustId(), record.getFullName(), record.getStatusCode());
    }
}

Everything downstream depends only on PartyManagement (a TMF632-shaped interface), unaware that a SOAP legacy system sits behind it.

Key Takeaways

  • Use Adapter when you must integrate a class whose interface doesn’t fit, without modifying its source.
  • Two flavors exist: object adapter (composition, shown above) and class adapter (inheritance) — favor composition in Java.
  • In enterprise telco integrations, Adapter is the standard way to expose legacy BSS/OSS systems through modern TMF Open API interfaces.