Here, we will explain Adapter Design Pattern with example in C#
The Adapter design pattern is a structural pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between two different interfaces, converting one interface into another expected by the client.
Incompatible Interfaces
Incompatible interfaces refer to situations where two software components or classes cannot work together directly because they have different methods, signatures, or formats for interacting with each other. This means the way one class provides its functionality does not match the way another class or client expects to receive or use it.
Example
FetchData()
to get data, but the client expects a method called
GetInfo()
.In a media player application, you have a client expecting an interface that can play MP3 files, but you need to integrate third-party libraries that can only play MP4 or VLC formats. The Adapter pattern allows you to wrap these incompatible media formats in a consistent interface so the client can play all of them through a unified interface.
Here is an illustration of a media player using the Adapter design pattern. It shows how different media formats (MP4, VLC) can be connected and adapted to a unified media player interface using adapters. This visually represents how incompatible formats can work together in a seamless system.
Below is an example using C# to demonstrate the Adapter Design Pattern in the context of a media player that plays MP3 files but integrates third-party libraries for MP4 and VLC formats. The adapter will allow these formats to work with a consistent interface.
using System;
// Target Interface
public interface IMediaPlayer
{
void Play(string fileName);
}
// Adaptee Class 1: Third-party MP4 player
public class Mp4Player
{
public void PlayMp4(string fileName)
{
Console.WriteLine("Playing MP4 file: " + fileName);
}
}
// Adaptee Class 2: Third-party VLC player
public class VlcPlayer
{
public void PlayVlc(string fileName)
{
Console.WriteLine("Playing VLC file: " + fileName);
}
}
// Adapter Class for MP4 Player
public class Mp4Adapter : IMediaPlayer
{
private Mp4Player _mp4Player;
public Mp4Adapter(Mp4Player mp4Player)
{
_mp4Player = mp4Player;
}
public void Play(string fileName)
{
_mp4Player.PlayMp4(fileName); // Adapted to use PlayMp4 method
}
}
// Adapter Class for VLC Player
public class VlcAdapter : IMediaPlayer
{
private VlcPlayer _vlcPlayer;
public VlcAdapter(VlcPlayer vlcPlayer)
{
_vlcPlayer = vlcPlayer;
}
public void Play(string fileName)
{
_vlcPlayer.PlayVlc(fileName); // Adapted to use PlayVlc method
}
}
// Client Code
public class MediaPlayerClient
{
private IMediaPlayer _mediaPlayer;
public MediaPlayerClient(IMediaPlayer mediaPlayer)
{
_mediaPlayer = mediaPlayer;
}
public void PlayMedia(string fileName)
{
_mediaPlayer.Play(fileName);
}
}
// Usage
class Program
{
static void Main(string[] args)
{
// Playing an MP4 file through the MP4 Adapter
Mp4Player mp4Player = new Mp4Player();
IMediaPlayer mp4Adapter = new Mp4Adapter(mp4Player);
MediaPlayerClient mp4Client = new MediaPlayerClient(mp4Adapter);
mp4Client.PlayMedia("movie.mp4");
// Playing a VLC file through the VLC Adapter
VlcPlayer vlcPlayer = new VlcPlayer();
IMediaPlayer vlcAdapter = new VlcAdapter(vlcPlayer);
MediaPlayerClient vlcClient = new MediaPlayerClient(vlcAdapter);
vlcClient.PlayMedia("documentary.vlc");
}
}
Electric Power Socket Adapter - In real life, different countries have different types of power sockets (e.g., Type A, B, C plugs). To use an appliance from one country in another with different sockets, we use a power adapter, which converts the socket type into one that is compatible with the appliance.
Here is the illustration that visually represents the concept of a power socket adapter, showing how it converts between different plug types (Type A, B, C) to fit an appliance.
Here's an implementation of the Adapter Design Pattern to represent the real-life example of power sockets and plug types.
In different countries, there are different types of power sockets (Type A, B, C). To use an appliance (like a charger) from one country in another country with a different socket type, we need an adapter to convert the plug type into one compatible with the socket.
We'll use the Adapter Design Pattern to adapt an incompatible plug to fit a socket.
ISocket
) - This is the
interface that defines the standard method for powering appliances,
regardless of the socket type.TypeAPlug
, TypeBPlug
)
-
Represent different plug types that are not directly compatible with the
socket.TypeAAdapter
, TypeBAdapter
)
-
These adapt the plug type to be used with a compatible socket.ISocket
interface to
power any appliance using different types of plugs through the adapters.using System;
// Target Interface: This represents a standard socket
public interface ISocket
{
void ProvidePower();
}
// Adaptee Class 1: A Type A plug appliance
public class TypeAPlug
{
public void InsertTypeAPlug()
{
Console.WriteLine("Type A plug inserted.");
}
}
// Adaptee Class 2: A Type B plug appliance
public class TypeBPlug
{
public void InsertTypeBPlug()
{
Console.WriteLine("Type B plug inserted.");
}
}
// Adapter Class for Type A Plug
public class TypeAAdapter : ISocket
{
private TypeAPlug _typeAPlug;
public TypeAAdapter(TypeAPlug typeAPlug)
{
_typeAPlug = typeAPlug;
}
public void ProvidePower()
{
_typeAPlug.InsertTypeAPlug(); // Adapts the Type A plug to the standard socket
Console.WriteLine("Power is being provided through Type A adapter.");
}
}
// Adapter Class for Type B Plug
public class TypeBAdapter : ISocket
{
private TypeBPlug _typeBPlug;
public TypeBAdapter(TypeBPlug typeBPlug)
{
_typeBPlug = typeBPlug;
}
public void ProvidePower()
{
_typeBPlug.InsertTypeBPlug(); // Adapts the Type B plug to the standard socket
Console.WriteLine("Power is being provided through Type B adapter.");
}
}
// Client Code: The socket expects to power any appliance using the ISocket interface
public class PowerSocketClient
{
private ISocket _socket;
public PowerSocketClient(ISocket socket)
{
_socket = socket;
}
public void PowerAppliance()
{
_socket.ProvidePower(); // Powers the appliance using the adapter
}
}
// Usage Example
class Program
{
static void Main(string[] args)
{
// Using a Type A plug in a standard socket via the Type A Adapter
TypeAPlug typeAPlug = new TypeAPlug();
ISocket typeAAdapter = new TypeAAdapter(typeAPlug);
PowerSocketClient clientA = new PowerSocketClient(typeAAdapter);
clientA.PowerAppliance(); // Type A plug adapts to the socket
// Using a Type B plug in a standard socket via the Type B Adapter
TypeBPlug typeBPlug = new TypeBPlug();
ISocket typeBAdapter = new TypeBAdapter(typeBPlug);
PowerSocketClient clientB = new PowerSocketClient(typeBAdapter);
clientB.PowerAppliance(); // Type B plug adapts to the socket
}
}
Output