Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Bridge pattern

Posted in Design Patterns
This post has been read 646 times

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

You can read about the other patterns from the following links

  1. Decorator Pattern
  2. Proxy Pattern

The Bridge pattern deals with removing the tight coupling of implementation of a class from its abstraction.  It helps the code to have backward functionality in the sense that, if there is a new version of implementation available, then the older version should not break i.e the older consumer of the code should not break.

    1 //abstraction

    2 public class Person

    3 {

    4     private IBridge _bridge;

    5 

    6     public Person(IBridge bridge)

    7     {

    8         _bridge = bridge;

    9     }

   10 

   11     public virtual void ShowFirstPerson()

   12     {

   13         _bridge.ShowFirst();

   14     }

   15 

   16     public virtual void ShowAllPersons()

   17     {

   18         _bridge.ShowAll();

   19     }

   20 }

   21 

   22 //bridge

   23 public interface IBridge

   24 {

   25     void ShowFirst();

   26     void ShowAll();

   27 }   

   28 

   29 //new abstraction

   30 public class PersonNewVersion : Person

   31 {

   32     public PersonNewVersion()

   33         : base(new DisplayPerson())

   34     {

   35     }

   36 

   37     public override void ShowAllPersons()

   38     {

   39         Console.WriteLine("New version of ShowAll");

   40         Console.WriteLine("----------------------");

   41         base.ShowAllPersons();

   42     }

   43 }

   44 

   45 //bridge implementor

   46 public class DisplayPerson : IBridge

   47 {

   48     private List<string> _personList;

   49 

   50     public DisplayPerson()

   51     {

   52         _personList = new List<string>();

   53         _personList.Add("Person1");

   54         _personList.Add("Person2");

   55         _personList.Add("Person3");

   56         _personList.Add("Person4");

   57     }

   58 

   59     public void ShowFirst()

   60     {

   61         Console.WriteLine(_personList.First());

   62     }

   63 

   64     public void ShowAll()

   65     {

   66         foreach (var item in _personList)

   67         {

   68             Console.WriteLine(item);

   69         }

   70     }       

   71 }

   72 

   73 public class BridgeProgram

   74 {

   75     public static void Main()

   76     {

   77         PersonNewVersion person = new PersonNewVersion();

   78 

   79         person.ShowFirstPerson();

   80         //Output : Person1

   81         person.ShowAllPersons();

   82         //Output : New version of ShowAll

   83         //----------------------

   84         //Person1

   85         //Person2

   86         //Person3

   87         //Person4

   88         Console.ReadLine();

   89     }

   90 }

We can see from the code that we can add a new implementation over the present implementation by a bridge and an abstraction layer and achieve more functionality over the original design. Bridge pattern can be used when we have to hide the implementation from the caller or when we have to change the implementation without changing the concrete  abstraction or when we know that certain operation might not always be implemented the same way.

In my next post I will be discussing about the Composite pattern.




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