Why Adapter Pattern
Recently I bought a laptop and found it difficult to work without a mouse. So I borrowed a mouse from my friend and to my surprise I couldn't plug it into my laptop. It was a PS/2 mouse, but my laptop supports only USB mouse. Now what do you think, a PS/2 to USB adapter to the rescue?
Figure 1: Mouse Laptop USB
Often times when we do development we encounter a situation where we have an old legacy interface that needs to fit into a new interface. In this case you will code a separate class that will make both the interfaces compatible with each other. This wrapper class provides seamless communication with both the old and new system. What you are implementing is in fact an adapter design pattern that in turn is a structural design pattern from the Gang of Four (GoF) software design patterns book.
Figure 2: Adapter Pattern
Implementation
Figure 3: Implement
In our case let's say we have a client class that contains the method Operate Laptop that takes an IUSB input as the parameter (refer to the preceding diagram). The IUSB interface is the new interface that expects objects of classes that implement IUSB. But we have only an object of the class that implements an IPS2. The PS2Mouse class is hence incompatible with the Operate Laptop method, since both exposes two different behaviors. This is like passing a square peg into a round hole. Completely incompatible, right?
Figure 4: Square Peg into a round hole
Figure 5: Connect Pass PS2 Mouse Instance
The first concept to understand when implementing an adapter pattern is to identify the source interface and target interface. In our case the source interface is IPS2 and the target interface is IUSB, both of them expose the OpenPS2 () and OpenUSB() methods respectively.
- interface IUSB
- {
- void OpenUSB();
- }
- interface IPS2
- {
- void OpenPS2();
- }
- class PS2Mouse : IPS2
- {
- public void OpenPS2()
- {
- Console.WriteLine("Opening..PS2 Mouse...");
- }
- }

Figure 6: Implemented IPS2 interface
- class PS2Adapter : IUSB
- {
- private IPS2 _input;
- public PS2Adapter(IPS2 input)
- {
- _input = input;
- }
- public void OpenUSB()
- {
- //before opeingin USB
- _input.OpenPS2();
- Console.WriteLine("Opening USB device");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- PS2Mouse mouse = new PS2Mouse();
- // OperateLaptop(mouse); // noncompatible
- PS2Adapter converter = new PS2Adapter(mouse);
- OperateUSBLaptop(converter); //compatible now
- }
- static void OperateUSBLaptop(IUSB device)
- {
- device.OpenUSB();
- }
- }

Figure 7: PS2Adapter
Since both interfaces are compatible we will get the following output:

Figure 8: Output

Santhakumar MunuswamyPosted Jun 6, 2015, 2:47 AM
Thanks for good work and well explained
Vithal WadjePosted Jun 5, 2015, 2:50 AM
well explained keep it up
Jasminder SinghPosted Jun 4, 2015, 11:50 AM
Ur implementation is correct but I think the code "PS2Mouse mouse = new PS2Mouse();" should have been done in the Adapter class rather than the client code or the adapter calling code. The reason being the client code should not be dealing/initializing the code which is already incompatible to interact with it.
Miguel Angel Ruiz AlvarezPosted Jun 4, 2015, 10:32 AM
Very well explained, thanks for sharing.
NitinPosted Jun 4, 2015, 9:40 AM
nice
Sibeesh VenuPosted Jun 4, 2015, 8:09 AM
Good one.