| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Adapter Design Pattern in C#
Jan 2, 2024
2 min read

Adapter Design Pattern in C#

Sponsor this Newsletter

Adapter Design Pattern :

Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

Analogy :

Imagine you have different electronic devices with various power plugs, and you want to connect them all to a single power outlet.

Instead of modifying the devices, you use plug adapters. These adapters act as intermediaries, allowing each device to connect to the outlet seamlessly.

Similarly, the Adapter Design Pattern lets incompatible classes work together by providing a wrapper (adapter) that bridges the gap between their interfaces.

When should we use it ?

Utilize the Adapter class when you need to incorporate an existing class into your code, but its interface doesn’t align with the rest of your system.

Advantages and Disadvantages By using adapter design pattern we fulfill S and O from SOLID Principles , although in some situations it can increase the complexity of code.

Enough talk, let’s see code in action.

How to implement Adapter in C#

Suppose we have some old code with in compatible interfaces.

public sealed class OldSystem
{
    public void DoSomethingOld()
    {
        Console.WriteLine("Doing something the old way.");
    }
}

And now we have a new interface expected by the rest of the code

public interface INewSystem
{
    void DoSomethingNew();
}

Let’s make an adapter class that makes OldSystem compatible with INewSystem

 public class Adapter : INewSystem
{
    private OldSystem _oldSystem;

    public Adapter(OldSystem oldSystem)
    {
        _oldSystem = oldSystem;
    }

    public void DoSomethingNew()
    {
        _oldSystem.DoSomethingOld();
    }
}

Let’s see now how can we utilise it.

class Program
{
    static void Main()
    {
        OldSystem oldSystem = new OldSystem();
        INewSystem adaptedSystem = new Adapter(oldSystem);
        adaptedSystem.DoSomethingNew();
    }
}

If you wanna learn more about design patterns here are three awesome resources

  • Design Patterns Series by Steve Smith on Pluralsight

  • Design Patterns Simplified eBook by Stefan Dokic

  • Refactoring Guru

P.S: These are not promotional links at all, I personally found them helpful

This article was originally published at https://mwaseemzakir.substack.com/ on Jan 2, 2024 .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.