How to Use Adapter Pattern in C#

Total Post:341

Points:2389
C# 
C#
 1618  View(s)
Ratings:
Rate this:
We want to  Use  Adapter Pattern in C#. How to use This Please Help me.
  1. Post:282

    Points:2008
    Re: How to Use Adapter Pattern in C#

    the adapter pattern is a  software design pattern that allows the interface of an existing class to be used from another interface.[1] It is often used to make existing classes work with others without modifying their source code.

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Prototype

    {

    class Example

    {

    static void Main()

    {

    var dataadapter = new DataAdapter();

    Connector databaseConnector = new DatabaseConnector();

    dataadapter.ImportData(databaseConnector);

    Connector xmlfileConnector = new XmlFileConnector();

    dataadapter.ImportData(xmlfileConnector);

    Connector httpstreamConnector = new ImageFileConnector();

    dataadapter.ImportData(httpstreamConnector);

    Console.ReadKey();

    }

    }

    public class DataAdapter

    {

    public void ImportData(Connector connector)

    {

    connector.GetData();

    }

    }

    public abstract class Connector

    {

    public abstract void GetData();

    }

    public class DatabaseConnector : Connector

    {

    public override void GetData()

    {

    var databaseHelper = new DataLoader();

    databaseHelper.QueryForChanges();

    }

    }

    public class XmlFileConnector : Connector

    {

    public override void GetData()

    {

    var xmlfileLoader = new XmlFileLoader();

    xmlfileLoader.LoadXML();

    }

    }

    public class ImageFileConnector : Connector

    {

    public override void GetData()

    {

    var ImageLoader = new ImageLoader();

    ImageLoader.GetImage();

    }

    }

    public class DataLoader

    {

    public void QueryForChanges()

    {

    Console.WriteLine("Data Loaded");

    }

    }

    public class ImageLoader

    {

    public void GetImage()

    {

    Console.WriteLine("Images Loaded.");

    }

    }

    public class XmlFileLoader

    {

    public void LoadXML()

    {

    Console.WriteLine("Xml files loaded.");

    }

    }

    }

     

  1. Post:1

    Points:7
    Re: How to Use Adapter Pattern in C#

    When you have a class that needs to utilize a particular interface, and you have a library that includes the functionality you need, but it doesn’t use the interface that you require. You can achieve the reuse of that library’s code by creating an Adapter class that sits between your client code, and the code that’s in this library, and adapts one interface to the other.

    Let’s look at the structure of the Adapter Pattern using this UML diagram. The two basic players within this example are the Client, and the Adaptee, shown below.Now,

    The Client needs some of the logic that exists within the Adaptee. Specifically, there is this AdaptedOperation that has the code that the Client wants to be able to utilize. Unfortunately, the Client has been written in such a way that it cannot directly call this AdaptedOperation because its interface is not the one that the Client expects. This is where the Adapter Pattern comes into play.

    First,

    The Adapter interface is created, exposing an Operation that has the interface the Client expects.

    Next,

    For each different implementation required, at a minimum, one, its different ConcreteAdapter is created that takes that Operation and implements it, such that that code calls the AdaptedOperation. In this way, the Client will now be able to call the Operation on the ConcreteAdapter, which in turn will call the AdaptedOperation on the Adaptee.

    The Client really wants to use the Adaptee directly, but unfortunately it can’t due to the incompatible interface. The Adapter Pattern is simply allowing us to achieve this despite this incompatibility.

    You can learn more about adapter design pattern in C# here.

Answer