Home > DeveloperSection > Forums > How to Use Adapter Pattern in C#
Ankit Singh

Total Post:341

Points:2389
Posted on    March-04-2016 11:24 PM

 C# C# 
Ratings:


 1 Reply(s)
 273  View(s)
Rate this:
We want to  Use  Adapter Pattern in C#. How to use This Please Help me.


aditya kumar Patel

Total Post:244

Points:1736
Posted on    March-04-2016 11:27 PM

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

}

}

}

 


Don't want to miss updates? Please click the below button!

Follow MindStick