Adaptor Pattern | Structural Design Pattern
There are two main types of adapters:
Class Adapter: Uses inheritance to adapt the interface of the Adaptee to the Target interface.
Object Adapter: Uses composition to contain an instance of the Adaptee and implements the Target interface by delegating calls to the Adaptee.
Example in C#:
______
using System;
// Target interface
public interface ITarget
{
void Request();
}
// Adaptee class with an incompatible interface
public class Adaptee
{
public void SpecificRequest()
{
Console.WriteLine("SpecificRequest called in Adaptee");
}
}
// Class Adapter (using inheritance)
public class ClassAdapter : Adaptee, ITarget
{
public void Request()
{
// Adapting the interface to meet the client's expectations
SpecificRequest();
}
}
// Object Adapter (using composition)
public class ObjectAdapter : ITarget
{
private readonly Adaptee _adaptee;
public ObjectAdapter(Adaptee adaptee)
{
_adaptee = adaptee;
}
public void Request()
{
// Adapting the interface to meet the client's expectations
_adaptee.SpecificRequest();
}
}
class Program
{
static void Main()
{
// Using the Class Adapter
ITarget classAdapter = new ClassAdapter();
classAdapter.Request();
// Using the Object Adapter
Adaptee adaptee = new Adaptee();
ITarget objectAdapter = new ObjectAdapter(adaptee);
objectAdapter.Request();
}
}
______
In this example:
ITarget is the target interface expected by the client.
Adaptee is the class with an incompatible interface.
ClassAdapter and ObjectAdapter are adapters that bridge the gap between the ITarget and Adaptee.
Adapters are especially useful when integrating existing code or third-party libraries with different interfaces, allowing them to work harmoniously within a system.
Real-world example involving voltage adapters for electronic devices.
Imagine you have a device designed to work with a specific voltage, say 110V (U.S.), but you're traveling to a country where the electrical system operates at a different voltage, for example, 220V(India).
Here's how the Adapter Pattern applies:
Target Interface (Device's Expected Interface): The device expects a power source with a voltage of 110V.
Adaptee (Existing System): The electrical system in the foreign country supplies power at 220V, which is incompatible with the device.
Adapter (Voltage Adapter): The voltage adapter acts as an interface between the device and the foreign electrical system. It adapts the 220V from the electrical system to the 110V expected by the device.
In this scenario:
Target Interface (110VDevice): Represents the interface expected by the device.
Adaptee (ForeignPowerSource): Represents the foreign electrical system with a different voltage.
Adapter (VoltageAdapter): Bridges the gap between the device and the foreign electrical system by adapting the voltage.
Example in C#:
______
using System;
// Target interface
public interface I110VDevice
{
void PlugInto110V();
}
// Adaptee (Existing System)
public class ForeignPowerSource
{
public void Supply220V()
{
Console.WriteLine("Supplying 220V");
}
}
// Adapter
public class VoltageAdapter : I110VDevice
{
private readonly ForeignPowerSource _foreignPowerSource;
public VoltageAdapter(ForeignPowerSource foreignPowerSource)
{
_foreignPowerSource = foreignPowerSource;
}
public void PlugInto110V()
{
Console.WriteLine("Adapter is converting 220V to 110V");
_foreignPowerSource.Supply220V();
Console.WriteLine("Device is now plugged into 110V");
}
}
// Client (Device)
public class Device : I110VDevice
{
public void PlugInto110V()
{
Console.WriteLine("Device is plugged into 110V");
}
}
class Program
{
static void Main()
{
// Using the Adapter to connect the Device to a foreign electrical system
ForeignPowerSource foreignPowerSource = new ForeignPowerSource();
I110VDevice device = new VoltageAdapter(foreignPowerSource);
// The Device interacts with the foreign electrical system through the Adapter
device.PlugInto110V();
}
}
______
In this example:
I110VDevice is the target interface representing the expected interface by the device.
ForeignPowerSource is the existing system with a different voltage.
VoltageAdapter is the adapter that bridges the gap, adapting the voltage to make the device compatible.
Device is the client representing the electronic device that expects 110V.
This real-world example illustrates how the Adapter Pattern can be applied to make incompatible systems work together.
Comments
Post a Comment