Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Adapter Pattern

Posted in Design Patterns
This post has been read 454 times

This is the continuing post in a series of post on Design patterns. Presently we are going through the structural patterns. I will be writing about the adapter pattern today.

You can read about the other patterns from the following links

  1. Decorator Pattern
  2. Proxy Pattern
  3. Bridge Pattern
  4. Composite Pattern
  5. Flyweight Pattern

Adapter pattern deals with providing a way to use an already present interface that does not meet with the present requirements. We create an adapter with our desired interface and make this class communicate with the classes that implement a different interface.


    1 public class AdapterPattern

    2 {

    3     public interface IPerson

    4     {

    5         string Name { get; set; }

    6         void MetroTravel();

    7     }

    8 

    9     public class Person : IPerson

   10     {

   11         public string Name { get; set; }

   12 

   13         public Person( string name )

   14         {

   15             Name = name;

   16         }

   17 

   18         public void MetroTravel()

   19         {

   20             Console.WriteLine( "Travel by Metro rail" );

   21         }

   22     }

   23 

   24     public interface INewPerson

   25     {

   26         string Name { get; set; }

   27         void CarTravel();

   28     }

   29 

   30     public class NewPerson : INewPerson

   31     {

   32         public string Name { get; set; }

   33         public NewPerson( string name )

   34         {

   35             Name = name;

   36         }

   37 

   38         public void CarTravel()

   39         {

   40             Console.WriteLine( "Travel by Car" );

   41         }

   42     }

   43 

   44     public class PersonAdapter : INewPerson

   45     {

   46         public IPerson _person;

   47         public string Name { get; set; }

   48 

   49         public PersonAdapter( IPerson person )

   50         {

   51             _person = person;

   52         }

   53 

   54         public void CarTravel()

   55         {

   56             _person.MetroTravel();

   57         }

   58 

   59     }

   60 

   61     public static void Main()

   62     {

   63         Person person = new Person( "Person1" );

   64 

   65         PersonAdapter adapter = new PersonAdapter( person );

   66         adapter.CarTravel();

   67         //Output : Travel by Metro rail

   68 

   69         Console.ReadLine();

   70     }

   71 }

As seen in the example, the Person class implements the IPerson interface which has a method MetroTravel. But we have a new interface called INewPerson which has a method called CarTravel. So if we have to use the new interface with the old class, we can create an adapter, PersonAdapter and use it as shown. We have to use an adapter when we have a class that has to connect to a mismatching interface or when we want to create a class which might interact with other class which are not yet built or when we have different methods for different purposes or when have have methods which change as called.

In my next post I will be writing about the Facade pattern which would bring us to the end of structural patterns.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll