- It convert the interface of a class into another interface clients expect.
- Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
- It can simply wrap the behavior to isolate a class from the underlying implementation.
- The adapter pattern is usually used when there is no control over the target class (adaptee).
Participants
Adaptee
(ComplexSystem)
- Defines an existing interface that needs adapting.
Target
(IAcceptedContract)
- Defines the domain-specific interface that client uses.
- This is an interface which is used by the client to achieve its functionality/request.
Adapter
(ComplexSystemAdapter)
- Adapts the interface
Adaptee
to the Target interface.
private static void Main()
{
IAcceptedContract adapter = new ComplexSystemAdapter();
foreach (string item in adapter.GetList())
Console.Write(item);
}
public class ComplexSystem
{
public string[][] GetComplexItems()
{
string[][] employees = new string[4][];
employees[0] = new string[] { "100", "Deepak", "Team Leader" };
employees[1] = new string[] { "101", "Rohit", "Developer" };
employees[2] = new string[] { "102", "Gautam", "Developer" };
employees[3] = new string[] { "103", "Dev", "Tester" };
return employees;
}
}
public interface IAcceptedContract
{
List<string> GetList();
}
public class ComplexSystemAdapter : ComplexSystem, IAcceptedContract
{
public List<string> GetList()
{
var employeeList = new List<string>();
var employees = GetComplexItems();
foreach (string[] employee in employees)
{
employeeList.Add(employee[0]);
employeeList.Add(employee[1]);
employeeList.Add(employee[2]);
}
return employeeList;
}
}
- You can achieve loose-coupling between adaptee and adapter by passing the adaptee instance to the constructor:
private static void Main()
{
var complexSystem = new ComplexSystem();
IAcceptedContract employeeAdapter = new ComplexSystemAdapter(complexSystem);
// ...
}
public class ComplexSystemAdapter : IAcceptedContract
{
private ComplexSystem _ComplexSystem;
public ComplexSystemAdapter(ComplexSystem complexSystem)
{
_ComplexSystem = complexSystem;
}
public List<string> GetList()
{
// ...
var employees = _ComplexSystem.GetComplexItems();
// ...
}
}
Rules of Thumb
- Adapter makes things work after they're designed.
- Bridge makes them work before they are.
- Bridge is designed up-front to let the abstraction and the implementation vary independently.
- Adapter is retrofitted to make unrelated classes work together.
- Adapter provides a different interface to its subject.
- Proxy provides the same interface.
- Decorator provides an enhanced interface.
- Adapter is meant to change the interface of an existing object.
- Decorator enhances another object without changing its interface.
- Decorator is thus more transparent to the application than an adapter is.
- As a consequence, Decorator supports recursive composition, which isn't possible with pure Adapters.
- Facade defines a new interface, whereas Adapter reuses an old interface.
- Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.