Learn Design Pattern - Bridge Pattern

Today's pattern is the Bridge Pattern. Last time we discussed the Flyweight Pattern

Agenda
 

  • What is Bridge Pattern?
  • Will go through the problem statement and see in which situation Bridge Pattern is preferred.
  • Implement a Bridge Pattern using an ASP.Net application.
  • Class Diagram.
Some more articles

    What is Bridge Pattern?

    As per the GOF the "Bridge Pattern decouples an abstraction from its implementation so that the two can vary independently".

    When to use Bridge Pattern?

    Real Life Example

    Electric equipment and their switches in our home are the best examples of the Bridge Pattern. For instance consider a Fan and its switch. Here the switch acts as an interface and the running of the fan is the actual implementation. Now the best part here is we can change the switch of the fan anytime, and also we can assign the previous switch to some other equipment.

    Practical Example

    Let's discuss the same scenario in terms of software.

    Assume we have to develop a system to draw a graphical structure like Rectangle or Circle.

    The End-user may use one of the two kinds of pencils, Red Pencil or Blue Pencil. And so ends with 4 combinations (Blue Rectangle, Blue Circle, Red Rectangle and Red Circle)

    The End output seems like this:

    Design-Bridge-Pattern.jpg

    One solution is to create 4 combinations of classes, each with its own kind of implementation but it's not a preferred solution as any new Pencil or shop means many new combinations.

     (I am about to start an online training for design patterns and other technical stuff, you can contact me at 9870148461 or at [email protected])

    Bridge Pattern as Solution

    In the Bridge Pattern we separate the Pencil and Shape from each other and try to connect them using Composition.

    Still Confused? Forget everything :) and let's start coding,

    Step 1: Create an AbstractShape (a Base for other Shapes):

    public abstract class AbstractShape
    {
           public abstract void Draw(string strColor);
    }

    Step 2: Create an AbstractPencil (a Base for other Pencils). Also create a Composition relationship between Pencil and Shape:

    public abstract class AbstarctPencil
    {
              protected AbstractShape objShape;
                       
              public AbstarctPencil(AbstractShape objShape)
              {
                       this.objShape = objShape;
              }
              public abstract void DrawWithColor();
    }

    Step 3: Create Concrete Pencils as:

    public class BluePencil : AbstarctPencil
    {
              public BluePencil(AbstractShape objShape):base(objShape)
              {
     
              }
              public override void DrawWithColor()
              {
                       objShape.Draw("Blue");
              }
    }
    public
     class RedPencil : AbstarctPencil
    {
              public RedPencil(AbstractShape objShape):base(objShape)
              {
     
              }
              public override void DrawWithColor()
              {
                       objShape.Draw("Red");
              }
    }

    Step 4: Create Concrete Shapes as:

    public class Circle : AbstractShape
    {
              public override void Draw(string strColor)
              {
                       HttpContext.Current.Response.Write("Circle in "+strColor+" Color<br><br>");
              }
    }
    public
     class Rectangle : AbstractShape
    {
              public override void Draw(string strColor)
              {

                       HttpContext.Current.Response.Write("Rectangle in " + strColor + " Color<br><br>");

              }
    }
    }

    Step 5: Time to create client code:

    I)AbstarctPencil objPencil = null;AbstractShape objShape = null;
    II)objShape = new Circle();objPencil = new RedPencil(objShape);
    III)objPencil.DrawWithColor();

    In II just replace Circle with rectangle, or just change RedPencil to blue or even if require change both.

    Both behave independently.

    (Please download the sample attached for complete understanding.)

    Class Diagram

    Class-Diagram-in-Bridge-Pattern.jpg

    Thanks for reading 
    Feedbacks and suggestions are always welcome.
    You can tweet me @SukeshMarla.

    Subscribe for my all article updates at
    https://www.facebook.com/pages/Blogs-By-Sukesh-Marla/168078149903213

    www.sukesh-Marla.com

     Hope all of you enjoyed reading this article.

    Up Next
      Ebook Download
      View all
      Learn
      View all