HOW TO USE ADAPTER PATTERN IN C#

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C# 
C#
 603  View(s)
Ratings:
Rate this:
We want to  Use  Adapter Pattern in C#. How to use This Please Help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    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.");

    }

    }

    }

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!