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
Lets say our application uses a component A and now we want to replace A with a component from 3rd party , say it as B.
Since the component B is not in our control which means we dont have control on its interface as well. But the problem here is that our client code is already using interface A and we dont want it to change. How can existing and unrelated classes work in an application that expects classes with a different and incompatible interface? This is the problem we will be going to resolve here using the Adapter pattern.
We will discuss two types of Adapters that can be used to use existing components in the form our client expects which are
- Class Adapter
- Object Adapter
Structure

(as defined by GoF)
Participants
Target
defines the domain-specific interface that Client uses.
Client
collaborates with objects conforming to the Target interface.
Adaptee
defines an existing interface that needs adapting.
Adapter
adapts the interface of Adaptee to the Target interface.
Example
Lets take an example to understand the above types of adapters
We have a weather forecasting application for USA and it fetches the local weather information for USA and displays to the client
It has exposed an interface which our application expects
//Target
interface IUSAWeather
{
string GetWeather();
}
Its pretty simple for the sake of explanation. It has just one method that returns a string information for the weather
and we have also an implementation for the above
//Original component
class USAWeather : IUSAWeather
{
public string GetWeather()
{
return "USA Weather";
}
}
Now consider a case that we have come across a situation that we have found a 3rd party component that provides more detailed information about the weather for USA and we want to use that instead. We also dont want our client code to expect a new interface. We can resolve this situation with adapter pattern
First lets see what 3rd party provides, We may not be aware of the implementation but only interface. For simplicity I ahve provided the implementation as well here
//Adaptee
class GlobalWeather
{
public string GetWeatherInfo(string countryName)
{
return "Weather for Country " + countryName + " is Cold Today";
}
}
Since the interface of "GlobalWeather" & 'IUSAWeather' are different, we have to create a Adapter so that our client code can work with 'IUSAWeather" as earlier and even then using the 3rd party component instead of our default implementation "USAWeather"
//Adapter
class WeatherAdapter : IUSAWeather
{
GlobalWeather _global;
public string GetWeather()
{
_global = new GlobalWeather();
return _global.GetWeatherInfo("USA");
}
}
You can see above that the adapter class now inherits from our original interface and using composition uses 3rd party interface "GlobalWeather"
This adapter simply redirects the call to the 3rd paty component and return the results
This is an example of "object adapter"
Now if we have to provide the soltion using the "class adapter" we can change our adapter class as follows
//Adapter
class WeatherAdapter : GlobalWeather, IUSAWeather
{
public string GetWeather()
{
return GetWeatherInfo("USA");
}
}
You can see above that our adapter now inherits directly from the 3rd party interface and also implements the interface that client expects
We can simply place call to the GetWeatherInfo() method our base class
Usage
Class Adapter
- It adapts Adaptee to Target by providing a concrete Adaptee class therefore it won't work when we want to adapt a class and all its subclasses.
- Adapter allows us to override some of Adaptee's behavior as it is a sub class.
Object Adapter
- It lets a single Adapter work with many Adaptees that is, the Adaptee itself and all of its subclasses
- It makes it harder to override Adaptee behavior
Related Patterns
Decorator is also used to provide the extended behaviors to an object. Decorator pattern enhances another object without changing its interface.Decorator supports recursive composition, which isn't possible with pure adapters.
Proxy may also be used to use a 3rd party interace. It defines a representative for another object and does not change its interface. But we have more options to change the