Adapter Design Pattern in C#
Mani Bhushan Shukla
Technology-neutral Architect experienced in Product Engineering, C#, Azure, AWS, Microservices, and Multitenant Applications. Specializes in Scalability, Digital Innovation, Cyber Security, and AI & ML.
The Adapter design pattern is a widely used and powerful pattern in software development. It facilitates the interaction between incompatible interfaces, enabling components with different interfaces to work together seamlessly. This pattern is also known as the wrapper pattern or translator pattern.
The Adapter Design Pattern is categorized as a Structural Design Pattern. According to the definition provided by the Gang of Four (GoF):
"The Adapter pattern converts the interface of a class into another interface that clients expect. It allows classes with incompatible interfaces to work together."
The term "Adapter" suggests that it allows two interfaces with mutual incompatibility to communicate with each other. To illustrate this, consider the example of a laptop charger. If you purchase a charger in the United States, it typically has flat pins that easily fit into US electrical sockets. However, when you travel to India, you may encounter round holes in electrical sockets. In such cases, you would use socket adapters or converters to make the charger compatible.
An example of the Adapter Design Pattern in C#:
领英推荐
// The Target interface defines the operations that the client expects
public interface ITarget
{
void Request();
}
// The Adaptee class implements the operations of a legacy system.
public class Adaptee
{
public void SpecificRequest()
{
// Do something specific.
}
}
// The Adapter class adapts the Adaptee class to the Target interface.
public class Adapter : ITarget
{
private Adaptee adaptee;
public Adapter(Adaptee adaptee)
{
this.adaptee = adaptee;
}
public void Request()
{
// Adapt the call to the Adaptee class.
adaptee.SpecificRequest();
}
}
// The Client class uses the Target interface to interact with the system.
public class Client
{
private ITarget target;
public Client(ITarget target)
{
this.target = target;
}
public void DoSomething()
{
// Call the Request() method on the Target interface.
target.Request();
}
}
// The following code demonstrates how the Client class can be used to interact with the Adaptee class.
public class Program
{
public static void Main(string[] args)
{
// Create an instance of the Adaptee class.
Adaptee adaptee = new Adaptee();
// Create an instance of the Adapter class.
Adapter adapter = new Adapter(adaptee);
// Create an instance of the Client class.
Client client = new Client(adapter);
// Call the DoSomething() method on the Client class.
client.DoSomething();
}
}.
In the code above, we have the following components:
In the Main method, we create an instance of the Adaptee class. Then, we create an instance of the Adapter class and pass the Adaptee instance to it. Finally, we create an instance of the Client class and execute the request through the adapter. The output demonstrates that the Adaptee's specific request is successfully executed through the Adapter.
This example illustrates how the Adapter Design Pattern enables the collaboration of incompatible interfaces by adapting the Adaptee's interface to match the desired Target interface.
I hope this is helpful!