Thursday, February 16, 2012

Design Pattern : Bridge Pattern Implemetation


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.

public class TextFileLogger : AbstractLogger
    {
        public override void Write(string info)
        {
            Console.WriteLine("Writing to file: " + info);
        }
    }

    public class EmailLogger : AbstractLogger
    {
        public override void Write(string info)
        {
            Console.WriteLine("Sending Email: " + info);
        }
    }


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

  public abstract class AbstractBL
    {
        public AbstractLogger Logger;

        public abstract void ProcessPayment();
    }

    public abstract class AbstractLogger
    {
        public abstract void Write(string info);
    }

    public class TransactionBL : AbstractBL
    {
        public override void ProcessPayment()
        {
            Logger.Write("Processing Payment");

            // Process Payment here
        }
    }

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.

Another Example:
The Bridge PatternThe bridge pattern decouples an abstraction from its implementation so the
two can vary independently. In other words we make a bridge between
the abstraction and its implementation and therefore we won't have a
binding between the two. The pattern helps us whenever we need to
select or switch the implementation at runtime.

Bridge pattern helps to decouple abstraction from implementation. With this if the implementation changes it does not affect abstraction and vice versa. Consider the figure ‘Abstraction and Implementation’. The switch is the abstraction and the electronic equipments are the implementations. The switch can be applied to any electronic equipment, so the switch is an abstract thinking while the equipments are implementations.


Figure: - Abstraction and Implementation
Let’s try to code the same switch and equipment example. First thing is we segregate the implementation and abstraction in to two different classes. Figure ‘Implementation’ shows how we have made an interface ‘IEquipment’ with ‘Start()’ and ‘Stop()’ methods. We have implemented two equipments one is the refrigerator and the other is the bulb.

Figure :- Implementation
The second part is the abstraction. Switch is the abstraction in our example. It has a ‘SetEquipment’ method which sets the object. The ‘On’ method calls the ‘Start’ method of the equipment and the ‘off’ calls the ‘stop’.

Figure: - Abstraction
Finally we see the client code. You can see we have created the implementation objects and the abstraction objects separately. We can use them in an isolated manner.

Figure :- Client code using bridge
Reference:

No comments:

Post a Comment