Definition
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Note:
- Wrap an existing class with a new interface.
- Impedance match an old component to a new system
- Adapter, the modifier, is about creating an intermediary abstraction that translates, or maps to the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.
- Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an improved interface.
Design
Please refer to the code
Some of best example: Adaptor for Two pin plug, We can have only one adaptor
UML Diagram
From GoF
Code
-
-
-
- public class AdaptorTarget
- {
- public virtual void Request()
- {
- Console.WriteLine("Called Target Request()");
- }
- }
-
-
-
-
- public class Adaptor : AdaptorTarget
- {
- internal OldSystem.Adaptee Adaptee
- {
- get { return new OldSystem.Adaptee();}
- set { ;}
- }
- public override void Request()
- {
- Adaptee.OldMethod();
- }
- }
-
- namespace OldSystem
- {
-
-
-
- internal class Adaptee
- {
- internal void OldMethod()
- {
- Console.WriteLine("Called incompatable system method....");
- }
- }
- }
Client
- internal static AdaptorTarget AdaptorTargetLocal
- {
- get
- {
- return new Adaptor();
- }
- set
- {
- }
- }
- static void Main(string[] args)
- {
- AdaptorTargetLocal.Request();
- }
Note: Adator provides wrapper for each system. Where are Façade does differently. And Proxy is also a wrapper but it exposes the same interfaces as the subsystem.
Hope this helps.