Adpater pattern converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
What is the practical usage of this pattern? Many frameworks use this pattern to convert different interfaces written in different languages so that they can communicate seamlessly and transparently with each other
Client.csusing System;using BusinessLogic;
namespace Test{ public class Client { public static void Main(string[] args) { VoltageAdapter Adapter = new VoltageAdapter(); Console.WriteLine(Adapter.Request(0)); Console.WriteLine(Adapter.Request(1)); Console.WriteLine(Adapter.Request(13)); } } }
BL.csusing System;
namespace BusinessLogic{ // Clients have no access to this class, but need its functionalities internal class ElectricShaver { internal string Request(int cable) { string Status = "Not yet plug in"; switch(cable) { case 0: Status = "110 Voltage goes here"; break; case 1: Status = "220 voltage goes here"; break; default: Status = "Kaboom"; break; } return Status; } }
// The VoltageAdapter is the adpater class providing access // to any internal or external interfaces that clients expect public class VoltageAdapter { private ElectricShaver adaptee = new ElectricShaver();
public string Request(int cable) { return adaptee.Request(cable); } }}
Compile the code as below:csc /t:library BL.cscsc /r:BL.dll Adapter.cs
Love to hear your point of view about this pattern.
Hope it helps
Yup I agree with your definition of Adapter pattern. However there are 2 variations in adapter patterns itself. Class Adapter and Object Adapter. Object Adapter use Composition while Class Adapter use multiple Inheritance concepts. The one that is being used is object Adapter pattern.
//Object Adapter pattern
http://devpinoy.org/blogs/cruizer