Design Patterns: Bridge

The Bridge Design Pattern allows you to decouple the implementation from the abstraction. It means that we can have the implementation separated from our classes and reuse them rather than implementing another hierarchy level.

One simple example mentioned on Stack Overflow is this structure:

2015-35-29 01-35-51-227

Implementing the Bridge Pattern it becomes:

2015-45-29 01-45-22-908

This simple example explains what it is and shows you why would you want it. But what about a more real world example? Well, we could for example rename the “Color” stuff to “DrawingApi”.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var hotApi = new HotDrawingImplementor();
            var coolApi = new CoolDrawingImplementor();

            var hotRectangle = new Rectangle(hotApi);
            var coolRectangle = new Rectangle(coolApi);

            hotRectangle.Draw();
            coolRectangle.Draw();

            Console.ReadKey(true);
        }
    }

    public abstract class Shape
    {
        protected DrawingImplementor implementor;

        public Shape(DrawingImplementor implementor)
        {
            this.implementor = implementor;
        }

        public void Draw()
        {
            implementor.Draw();
        }
    }

    public class Rectangle : Shape
    {
        public Rectangle(DrawingImplementor implementor)
            : base(implementor)
        {

        }
    }

    public class Circle : Shape
    {
        public Circle(DrawingImplementor implementor)
            : base(implementor)
        {
        }
    }

    public abstract class DrawingImplementor
    {
        public abstract void Draw();
    }

    public class CoolDrawingImplementor : DrawingImplementor
    {
        public override void Draw()
        {
            Console.WriteLine("Drawing cool!");
        }
    }

    public class HotDrawingImplementor : DrawingImplementor
    {
        public override void Draw()
        {
            Console.WriteLine("Drawing hot!");
        }
    }
}

Output:

Drawing hot!
Drawing cool!

If you have a hierarchy like that you might want to consider applying this pattern.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s