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.
theadapter patternis a software design pattern that allows theinterfaceof an existingclassto be used from another interface.[1]It is often used to make existing classes work with others without modifying theirsource code.
Shadman Kudchikar
03-Feb-2019When 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.
Anonymous User
04-Mar-2016the 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
{
classExample
{
staticvoid Main()
{
var dataadapter = newDataAdapter();
Connector databaseConnector = newDatabaseConnector();
dataadapter.ImportData(databaseConnector);
Connector xmlfileConnector = newXmlFileConnector();
dataadapter.ImportData(xmlfileConnector);
Connector httpstreamConnector = newImageFileConnector();
dataadapter.ImportData(httpstreamConnector);
Console.ReadKey();
}
}
publicclassDataAdapter
{
publicvoid ImportData(Connector connector)
{
connector.GetData();
}
}
publicabstractclassConnector
{
publicabstractvoid GetData();
}
publicclassDatabaseConnector : Connector
{
publicoverridevoid GetData()
{
var databaseHelper = newDataLoader();
databaseHelper.QueryForChanges();
}
}
publicclassXmlFileConnector : Connector
{
publicoverridevoid 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.");
}
}
}