Adapter Pattern in C# – Real-Time Example (Compatibility Between Systems)

What is Adapter Pattern?

The Adapter Pattern is a structural design pattern that converts one interface into another compatible interface so that incompatible classes can work together.

It acts like a bridge or translator between two systems.


Why Use Adapter Pattern?

  • Integrate third-party APIs easily
  • Reuse legacy code
  • Avoid modifying existing systems
  • Standardize external integrations

Real-Time Scenario

Suppose your application expects:

IPayment.Pay()

But a third-party provider gives:

MakePayment()

👉 Interfaces are different
👉 Adapter solves this compatibility issue


Real-Time Example – Payment Gateway Integration


Step 1: Target Interface (Expected by System)

public interface IPayment
{
    void Pay(decimal amount);
}

Step 2: Third-Party Service (Incompatible Interface)

public class ThirdPartyPaymentGateway
{
    public void MakePayment(decimal amount)
    {
        Console.WriteLine($"Paid {amount} using third-party gateway");
    }
}

Step 3: Adapter Class

public class PaymentAdapter : IPayment
{
    private readonly ThirdPartyPaymentGateway _gateway;

    public PaymentAdapter(ThirdPartyPaymentGateway gateway)
    {
        _gateway = gateway;
    }

    public void Pay(decimal amount)
    {
        _gateway.MakePayment(amount);
    }
}

Usage Example

IPayment payment = new PaymentAdapter(
    new ThirdPartyPaymentGateway()
);

payment.Pay(1000);

Output

Paid 1000 using third-party gateway

Key Concept

Instead of changing third-party code ❌
We adapt it ✔

payment.Pay(1000);

// internally converted to
_gateway.MakePayment(1000);

Diagram Understanding

Client
   ↓
IPayment
   ↓
PaymentAdapter
   ↓
ThirdPartyPaymentGateway

👉 Adapter translates communication


Advantages

  • ✔ Reuse existing systems
  • ✔ No modification of third-party code
  • ✔ Improves flexibility
  • ✔ Reduces coupling
  • ✔ Easy integration of external APIs

Disadvantages

  • ✖ Adds extra abstraction layer
  • ✖ More classes introduced
  • ✖ Can increase complexity if overused

When to Use

Use Adapter Pattern when:

  • Integrating third-party libraries
  • Working with legacy systems
  • Interfaces are incompatible
  • You cannot modify external code

Real Project Mapping (.NET + Angular)

Feature Usage
Payment gateway integration Adapter
External REST APIs Adapter
Legacy ERP integration Adapter
Vendor SDK integration Adapter

Advanced .NET Example

Using Multiple Providers

public class RazorpayAdapter : IPayment
{
    public void Pay(decimal amount)
    {
        Console.WriteLine("Razorpay Payment");
    }
}

public class StripeAdapter : IPayment
{
    public void Pay(decimal amount)
    {
        Console.WriteLine("Stripe Payment");
    }
}

👉 Easily switch providers without changing business logic


Pro Tip

Combine:

  • Adapter + Factory Pattern
  • Adapter + Dependency Injection

For scalable enterprise applications.


Summary

Adapter Pattern helps you:

  • Connect incompatible systems
  • Integrate third-party services cleanly
  • Reuse legacy code efficiently

👉 Perfect for:

  • Payment gateways
  • External APIs
  • Legacy integrations
  • Multi-provider systems

Comments

Popular posts from this blog

Promises in Angular

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Csharp Coding - Session