SIGN UP MEMBER LOGIN:    
ARTICLE

Bridge Pattern

Posted by Jean Paul Articles | Design & Architecture January 27, 2012
In this article we can explore the Bridge pattern which is commonly used in applications.
Reader Level:
Download Files:
 

In this article we can explore the Bridge pattern which is commonly used in applications.

Challenge

You are working on an abstract business class. The class needs to log information to by writing to a file, email to user etc. We need to avoid tightly coupling of the business class and logger as in future newer versions of logger might appear.

BrdPtrn1.gif

How to achieve a decoupled solution?

Definition

Decouple an abstraction from its implementation so that the two can vary independently

Implementation

We can create an abstract class for representing the Logger. Let the abstract business class hold reference to the new abstract Logger. In the runtime we can specify the actual implementation instance of Logger class. (File writer or Emailer)

Following is the snapshot of the class diagram after using the Bridge Pattern:

BrdPtrn2.gif

Here the concrete Logger implementations derive from AbstractLogger class.

The concreate TransactionBL derives from AbstractBL class. The AbstractBL class is holding reference to Logger (abstract Logger).

Using the Bridge Pattern as shown above, there is decoupling of the implementation from its abstraction. It allows future extensibility to the Logger classes without modifying the BL class.

Code

Following is the code that invokes above Transaction and Logger classes.

AbstractBL bl = new TransactionBL();
bl.Logger = new TextFileLogger();
bl.ProcessPayment();

bl.Logger = new EmailLogger();
bl.ProcessPayment();

Console.ReadKey(false);

On running the application we can see the following output.

BrdPtrn3.gif

Following is the coupling snapshot after using the Bridge Pattern.

BrdPtrn4.gif

Note

We can see that this pattern is a simple implementation using an abstract class and interface. The Bridge Pattern is having some similarity with Strategy Pattern as both involve changing the implementation in the runtime. Here the class is abstract when compared with Strategy pattern.

Summary

In this article we have explored the Bridge design pattern. It provides convenient way for separating the abstraction from implementation through a bridge of abstract class or interface. The attached source code contains the example we discussed.
 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Stephen for the good words.. I hope to do better.

Posted by JEAN Paul Jan 31, 2012

Hi Jean. You have presented your article very nicely.

Posted by Stephen Johnson Jan 30, 2012

Thank you for sharing

Posted by Sonakshi Singh Jan 30, 2012

Thank You Sonakshi for the encouragements..

Posted by Jean Paul Jan 28, 2012

very comprehensive

Posted by Sonakshi Singh Jan 27, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor