Bridge Pattern with C# example

By | January 30, 2017

In this article I will discuss about the bridge pattern in C# with practical example. Bridge pattern is also an structural pattern like Proxy and decorator patterns.

Become an Expert in Programming and Web Development.View all courses!

You may read other articles in this series at below links.

  1. Proxy pattern 
  2. Decorator pattern
  3. SOLID principles

What is Bridge Pattern?

A bridge pattern in used to decouple abstraction from its implementation enabling them to vary independently. This is the pattern which helps the clients to maintain the versioning of the software. If a new version of the software is released it should not break the older clients functionalities. There is no need to change the client code for adhering to the newer version. But the client need to specify which version it has to use.

Practical Example of Bridge Pattern

Lets see what happens if I am not using the Bridge pattern in my application.

Problem:

Myself begin an avid reader, I have developed an reader App which is supported by various windows versions.

Lets check the code first and I will explain it later.

I have developed an ReadingApp class. First version of the class only supported the Display() method. This method will only display the the text as it is. The class is implemented and modified with some code in Windows8 and Windows10 platforms.

Now with the new version of the class I want the reader to read the text in front of the mirror. For the same reason I will include a new method into the class named ReverseDisplay().

Now to read the reverse text in front of the mirror I need to do lot of changes at the client side. Also If I want to change any of the method I have to change all the classes. Suppose I want to shown only a word per row. I have to do the desired change at each class.

Solution:

To overcome the disadvantages such scenarios bridge pattern comes to the rescue. Lets refactor the classes to achieve more decoupled code. This should help us to change the abstractions independently. Lets check the code in below snippet.

As we can see in the above code. I have abstracted out the display functionality. And the concrete windows 8 and windows 10 class implement the Reading App. The implementation of the Display method is totally independent  of the abstraction. To call any of the implementation of the display method there are not many changes done on the client side as well.

Check the client code below.

As you can see in the code above I just need to provide the correct formatter at the run time to get the correct display.

To implement the bridge pattern we need to identify the operations that are not always implemented in the same way.

Also we should implement the bridge pattern when we want to

  • completely hide the implementation from the client.
  • Avoid binding an implementation to an abstraction directly.
  • Change an implementation even without recompiling an abstraction.
  • Combine different parts of the system at runtime.

Conclusion:

In this article I have discussed about the bridge pattern with a simple C# code example. This should help you to understand how implementation can be separated from abstraction.