Post Details

What is the Adapter Design Pattern in C Sharp .Net ?

np

Fri , Aug 04 2023

np

Article 2 on design patterns-

 The Adapter Design Pattern

The Adapter Design Pattern is a structural design pattern that converts the interface of a class into another interface that clients expect. It is used when two incompatible interfaces need to work together. Here is an example of the Adapter Design Pattern in C#:

using System;


public interface ITarget

{

    void Request();

}


public class Adaptee

{

    public void SpecificRequest()

    {

        Console.WriteLine("Adaptee method called.");

    }

}


public class Adapter : ITarget

{

    private readonly Adaptee _adaptee;


    public Adapter(Adaptee adaptee)

    {

        _adaptee = adaptee;

    }


    public void Request()

    {

        _adaptee.SpecificRequest();

    }

}


class Program

{

    static void Main()

    {

        Adaptee adaptee = new Adaptee();

        ITarget target = new Adapter(adaptee);

        target.Request();


        Console.ReadKey();

    }

}



Leave a Reply

Please log in to Comment On this post.