What is the Adapter Pattern?
The Adapter pattern is a structural design pattern which enables a system to use classes whose interfaces don’t quite match its requirements or in other words is used to make an interface which the client understands.
When to Use It?
- It's quite useful when dealing with the legacy code especially that was written a while ago and to which one might not have access.
- It is especially useful for off-the-shelf code, for toolkits, for libraries or any third party software.
Many of us use the enterprise library data access application block. Suppose later if we find that some other third party data access application block is better than Microsoft data access application block, then the project would not change just adapter internally would call the new data access application block.
Object Adapter Pattern
In Object adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.
public bool IsEmail(string email)
bool ValidateEmail(string email);
class Adapter : Adaptee, ITarget
public bool ValidateEmail(string email)
static void Main()
Adaptee first = new Adaptee();
Console.Write("Before the new standard: ");
ITarget second = new Adapter();
Console.WriteLine("\nMoving to the new standard");
ITarget: The interface that the Client wants to use
Adaptee: An implementation that needs adapting
Adapter: The class that implements the
ITarget interface in terms of the Adaptee
ValidateEmail: An operation that the Client wants
IsEmail: The implementation of Request’s functionality in the Adaptee
The adapter pattern is also useful where an already existing class provides some or all of the services you need but does not use the interface you need. Also using this way, you can change third-party library easily without affecting your project. In the above example, if you want to change the validation method of email, all you need to do is to use
ValidateEmail function for any new method or third party library functions.
View this article on my blog.
Your feedback is welcome.