Bridge – design patterns

Bridge is a structural pattern which decouples abstraction from implementation. Uses composition rather than inheritance.
Imagine you have two shapes: circle and triangle. They can be colored with two colors: black and red. Imagine you want to implement simple inheritance. Base class will be shape. Circle and triangle inherits from shape. RedCircle, BlackCircle, RedTriangle, BlackTriangle inherit from circle and triangle.
Imagine you would have 10 different shapes and 10 different colors. You would end up with 100 classes (RedRectangle, BlueEllpse, PinkSquare, etc.).
To avoid such situation we would implement bridge. We can decouple two different things – shapes and colors.
We can implement concrete Circle and Triangle which base on abstract Shape class. On the other hand we can implement RedColor and BlackColor on IColor interface. We pass instance of color in shape constructor.
See example below.

Our colors.

namespace DesignPatterns.Bridge
{
    /// <summary>
    /// Interface for colors.
    /// </summary>
    interface IColor
    {
        void Apply();
    }
}
[/sourcecode language="csharp"]


using System;

namespace DesignPatterns.Bridge
{
    /// <summary>
    /// Represents red color
    /// </summary>
    class RedColor : IColor
    {
        public void Apply()
        {
            Console.WriteLine("Filling with red.");
        }
    }
}


using System;

namespace DesignPatterns.Bridge
{

    /// <summary>
    /// Represents black color.
    /// </summary>
    class BlackColor : IColor
    {
        public void Apply()
        {
            Console.WriteLine("Filling with black.");
        }
    }
}

Our shapes. Reference to IColor is passed in constructor. It is used in DrawColored method.

namespace DesignPatterns.Bridge
{
    /// <summary>
    /// Interface for shapes. Handles reference to color.
    /// </summary>
    abstract class Shape
    {
        protected IColor _color;

        /// <summary>
        /// Constructor with passed color.
        /// </summary>
        /// <param name="color"></param>
        public Shape(IColor color)
        {
            _color = color;
        }

        /// <summary>
        /// Draws shape using color given in constructor.
        /// </summary>
        public abstract void DrawColored();
    }
}
using System;
using System.Drawing;

namespace DesignPatterns.Bridge
{
    /// <summary>
    /// Circle - concrete implementation of shape
    /// </summary>
    class Circle : Shape
    {
        public Circle(IColor color, Point center, int radius) : base(color)
        {
            Center = center;
            Radius = radius;
        }

        public Point Center { get; private set; }
        public int Radius { get; private set; }

        public override void DrawColored()
        {
            _color.Apply();
            Console.WriteLine($"Drawing circle with applied color.");
        }
    }
}


using System;
using System.Drawing;

namespace DesignPatterns.Bridge
{
    /// <summary>
    /// Triangle - concrete implementation of shape
    /// </summary>
    class Triangle : Shape
    {
        public Triangle(IColor color, Point a, Point b, Point c) : base(color)
        {
            A = a;
            B = b;
            C = c;
        }

        public Point A { get; private set; }
        public Point B { get; private set; }
        public Point C { get; private set; }

        public override void DrawColored()
        {
            _color.Apply();
            Console.WriteLine($"Drawing triangle with applied color");
        }
    }
}

Usage of the bridge. Three shapes are created with two different colors.

        public static void BridgeUsage()
        {
            var redCircle = new Bridge.Circle(new Bridge.RedColor(), new System.Drawing.Point(0, 0), 10);
            var blackCircle = new Bridge.Circle(new Bridge.BlackColor(), new System.Drawing.Point(0, 0), 10);
            var redTriangle = new Bridge.Triangle(new Bridge.RedColor(), new System.Drawing.Point(10, 0), new System.Drawing.Point(0, 10), new System.Drawing.Point(5, 5));

            redCircle.DrawColored();
            blackCircle.DrawColored();
            redTriangle.DrawColored();
        }
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