Adapter pattern in C#

 Hello, C# lovers! 😍

Today, I want to talk about one of the most useful and versatile design patterns in C#: the Adapter pattern. 🙌

The Adapter pattern is a structural pattern that allows two incompatible interfaces to work together by providing a common interface between them. 🤝

This pattern is very helpful when you want to reuse some existing classes or libraries that have different interfaces than what your project expects. For example, you may want to use a third-party library that provides some functionality that you need, but its interface is different from the one you have defined in your project. Or, you may want to use some legacy code that has a different interface than the new code you are writing. 😊

The Adapter pattern solves this problem by introducing an additional class called the Adapter, which implements the expected interface and delegates the calls to the existing class. This way, you can use the existing class without changing its interface or modifying your code. 🙌

Let’s see how this pattern works with a simple example. Suppose you have an interface called ITarget that defines some methods that your project expects:


// The 'Target' interface
public interface ITarget
{
void Request();
}


Now, suppose you have a class called Adaptee that provides some functionality that you want to use, but its interface is different from ITarget:


// The 'Adaptee' class
public class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()");
}
}


To use the Adaptee class in your project, you need to create an Adapter class that implements ITarget and wraps an instance of Adaptee:


// The 'Adapter' class
public class Adapter : ITarget
{
private readonly Adaptee _adaptee = new Adaptee();

public void Request()
{
// Possibly do some other work
// and then call SpecificRequest
_adaptee.SpecificRequest();
}
}


Now, you can use the Adapter class as if it was an ITarget:


// The 'Client' class
public class Client
{
static void Main(string[] args)
{
// Create adapter and place a request
ITarget target = new Adapter();
target.Request();

// Wait for user
Console.ReadKey();
}
}


The output of this program is:


Called SpecificRequest()

As you can see, the Adapter pattern allows you to use the Adaptee class without changing its interface or modifying your code. You can also easily switch to a different Adaptee class by changing the Adapter implementation. 😎

The Adapter pattern is widely used in C# and .NET Framework. For example, the System.Data.IDataReader interface is an adapter for various data sources, such as SQL Server, Oracle, ODBC, etc. The System.IO.StreamReader and System.IO.StreamWriter classes are adapters for reading and writing text from different streams, such as files, network sockets, memory buffers, etc. The System.Collections.IEnumerator interface is an adapter for iterating over different collections, such as arrays, lists, dictionaries, etc. 😍

If you want to learn more about the Adapter pattern and see some real-world examples in C#, I recommend you to check out these resources:

I hope you enjoyed this article and learned something new about the Adapter pattern. If you have any questions or feedback, please leave a comment below. And don’t forget to share this article with your friends and colleagues who love C#. 😊


You can find the code examples here: source code


Stay tuned for more articles on C# design patterns. Happy coding! 😊

Comments

Popular posts from this blog

Which GOF patterns are good for C#?

Proxy pattern in C#

Bridge pattern in C#