Click here to Skip to main content
12,954,856 members (61,007 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 3 Aug 2012

Understanding and Implementing Bridge Pattern in C#

, 3 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This article discuss Bridge Pattern and try to demonstrate it using a simple application.
This is an old version of the currently published tip/trick.


Let us discuss "Bridge Pattern" in this article. When we think about bridge pattern the first thing that come to our mind is "it is some kind of bridge between two different implementations". But this is not true. The above mentioned This is the task of Adapter pattern which is discussed here. But to talk about bridge, this pattern is designed specifically to let the abstraction and the implementation vary independently.


GoF defines Bridge Pattern as "Decouple an abstraction from its implementation so that the two can vary independently."

The best example for where this pattern can be used is the use of plugins or driver. If we have an application that can take use of any specific driver then we can use bridge pattern. A classic example of a scenario where this pattern could be used in a Video Configuration selection screen of a game like "Half-life". This screen gives the user an option to run the game in OpenGL, Direct3D or software mode. When the user makes a selection then we can use a concrete implementation specific to the selection and not effect the abstraction at all.

Let us visualize the bridge pattern now. 

Using the Code

let us now try to implement a small example using bridge pattern. Let us think of an imaginary smart TV that gives the user a possibility of playing local cable TV, a local satellite TV or an IPTV source. Here the user can request TV guide from the selected source. He can also choose to play the selected TV source.  

So from the TV's perspective the abstraction for getting the guide and play the TV. The actual implementation will differ for each source. So let us go ahead and first define the abstraction that each TV source will provide i.e. The Implementor from the above diagram.

interface IVideoSource
	string GetTvGuide();
	string PlayVideo();

Let us now define the ConcreteImplementors for each TV source.

class LocalCabelTv : IVideoSource
	const string SOURCE_NAME = "Local Cabel TV";

	string IVideoSource.GetTvGuide()
		return string.Format("Getting TV guide from - {0}", SOURCE_NAME);

	string IVideoSource.PlayVideo()
		return string.Format("Playing - {0}", SOURCE_NAME);

class LocalDishTv : IVideoSource
	const string SOURCE_NAME = "Local DISH TV";

	string IVideoSource.GetTvGuide()
		return string.Format("Getting TV guide from - {0}", SOURCE_NAME);

	string IVideoSource.PlayVideo()
		return string.Format("Playing - {0}", SOURCE_NAME);

class IPTvService : IVideoSource
	const string SOURCE_NAME = "IP TV";

	string IVideoSource.GetTvGuide()
		return string.Format("Getting TV guide from - {0}", SOURCE_NAME);

	string IVideoSource.PlayVideo()
		return string.Format("Playing - {0}", SOURCE_NAME);

So we can see that each ConcreteImplementor provide the same abstraction but the actual implementation differs (Right now we have dummy messages to show the different implementations).

Next thing would be to use the appropriate implementation based on user selection. So let us give the user a choice to select the appropriate TV source and then get the tv guide from that source and even start playing that source.

class SuperSmartTvController
	static void Main(string[] args)
		MySuperSmartTV myTv = new MySuperSmartTV();
		Console.WriteLine("Select A source to get TV Guide and Play");
		Console.WriteLine("1. Local Cable TV\n2. Local Dish TV\n3. IP TV");

		ConsoleKeyInfo input = Console.ReadKey();

		// Let us see what user has selected and select the video source accrodingly
		switch (input.KeyChar)
			case '1':
				myTv.VideoSource = new LocalCabelTv();

			case '2':
				myTv.VideoSource = new LocalDishTv();

			case '3':
				myTv.VideoSource = new IPTvService();

		Console.WriteLine(); //some whitespace on output for readability

		//Let us show the TV guide from selected source

		//Let us now play the selected TV source.

		Console.WriteLine(); //some whitespace on output for readability

Now the user can select the TV source and each TV source will have its own implementation but can be used with the same abstraction like a plugin based architecture.The interface for TV source now is totally independent of the implementations of each TV source.

Let us run the application and select IPTV as the source

Point of Interest

Since the interface are more stable (i.e less probability of getting changed) than the implementation it sounds like a good idea to put the interfaces of a class separate from the implementation.


  • 04 August 2012: First Version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahul Rajat Singh
Software Developer (Senior)
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more:[^]

  • Microsoft MVP 2015

You may also be interested in...


Comments and Discussions

Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
QuestionNice explanation Pin
vinod.jangle10-Jul-15 12:38
membervinod.jangle10-Jul-15 12:38 
QuestionGood project Pin
Vyacheslav Krakovsky23-Dec-14 4:54
memberVyacheslav Krakovsky23-Dec-14 4:54 
GeneralSuperb ! Pin
Octavio Tobar A23-May-14 10:27
memberOctavio Tobar A23-May-14 10:27 
GeneralGood example! Pin
Gena H23-Mar-14 1:48
memberGena H23-Mar-14 1:48 
Question[My vote of 2] Bridge? Pin
zzfima12-Nov-13 5:57
memberzzfima12-Nov-13 5:57 
QuestionCorrect example ? Pin
Member 1019457613-Oct-13 10:26
memberMember 1019457613-Oct-13 10:26 
QuestionNice but abstraction is missing Pin
eswarpmca19-Dec-12 18:54
membereswarpmca19-Dec-12 18:54 
QuestionIsnt it a Factory Pattern ? Pin
vijaysutaria6-Aug-12 3:05
membervijaysutaria6-Aug-12 3:05 
QuestionGood Example but... Pin
Sanjay K. Gupta3-Aug-12 21:13
memberSanjay K. Gupta3-Aug-12 21:13 
AnswerRe: Good Example but... Pin
Rahul Rajat Singh3-Aug-12 21:59
memberRahul Rajat Singh3-Aug-12 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Aug 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid