Bridge Pattern

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.

clip_image002

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:

clip_image004

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.

clip_image006

Following is the coupling snapshot after using the Bridge Pattern.

clip_image008

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.

Leave a Reply

Your email address will not be published. Required fields are marked *