Adapter Pattern


Adapter pattern Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Also known as Wrapper

Example and reasons.
Dot Net library, we cannot change the library interface, since we may not have its source code
Even if we did have the source code, we probably should not change the library for each domain-specific application

We will create our adapter for our domain that adapts the interface to match our needed interface.
•         Target
•         Adapter
•         Adaptee
•         Client
•         Factory
Class adapters
•     Simple and invisible to the client.
•     A class adapter has instance of an interface and inherits a class.
•     Overriding behavior can be done more easily with a Class adapter
Use the Adapter pattern when you want to:
•     Create a reusable class to cooperate with yet-to-be-built classes.
•     Change the names of methods as called and as implemented.
•     Use an existing class, and its interface does not match the one you need.


public class Client1
public void AnyMethod()
//Target t = Factory.CreateTarget();
Target t = new Target();
//Client1 use the the target interface “Request”

public class Target
public virtual void Request()
Console.WriteLine(“Called Target Request()”);

public class Adapter : Target
private Adaptee _adaptee = new Adaptee();
public override void Request()
// Possibly do some other work
//  and then call SpecificRequest

public class Adaptee
public void SpecificRequest()
Console.WriteLine(“Called SpecificRequest()”);
public class Factory
public static Target CreateTarget()
return new Adapter();
//return new Target();


Share This: