DCSIMG
Bridge Pattern - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Bridge Pattern

Bridge Pattern

In this post I'm going to explain the use of the bridge pattern.
You can read my previous posts on structural patterns here:
Decorator pattern 
Proxy pattern
Facade pattern
Adapter pattern
Composite pattern

The Bridge Pattern
The bridge pattern decouples an abstraction from it's implementation so the
two can vary independently. In other words we make a bridge between
the abstraction and it's 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.
For a UML diagram of the pattern go to dofactory site.

Example in C#
How does it work? Lets look at a small example.

    #region The Abstarction

 

    public class Abstraction

    {

        #region Members

 

        private Bridge _bridge;

 

        #endregion

 

        #region Ctor

 

        /// <summary>

        /// Construct a new Abstraction object with

        /// the given bridge

        /// </summary>

        /// <param name="bridge">The given bridge</param>

        public Abstraction(Bridge bridge)

        {

            _bridge = bridge;

        }

 

        #endregion

 

        #region Methods

 

        /// <summary>

        /// The method demonstrate the call for

        /// the bridge object by its abstraction

        /// </summary>

        public void Operaion()

        {

            Console.Write("Using");

            _bridge.OperationImplementation();

        }

 

        #endregion

    }

 

    #endregion

 

    #region The Bridge And Its Implementations

 

    public interface Bridge

    {

        void OperationImplementation();

    }

 

    public class BridgeImplementationA : Bridge

    {

        #region Bridge Members

 

        /// <summary>

        /// Perform implementation A operation

        /// </summary>

        public void OperationImplementation()

        {

            Console.Write("BridgeImplementationA");

        }

 

        #endregion

    }

 

    public class BridgeImplementationB : Bridge

    {

        #region Bridge Members

 

        /// <summary>

        /// Perform implementation B operation

        /// </summary>

        public void OperationImplementation()

        {

            Console.Write("BridgeImplementationB");

        }

 

        #endregion

    }

 

    #endregion

You can see that the abstraction holds a bridge object and gets the bridge in the
constructor. Therefore, whenever one of the implementation is needed you pass it
in the constructor and you won't be coupled to the implementation.

Summary
To sum up the post, use the bridge pattern whenever you identify that the operations
you write not always need to be implemented in the same way.
Another reason is not to bind the abstraction to its implementation.
In the next post I'll cover the last pattern in the structural patterns -
the flyweight pattern.

Comments

Gil Fink Blog said:

The first pattern in the creational pattern series is the singleton pattern. You can read my previous

# April 23, 2008 9:33 AM

Yaron Shkop said:

I like clear and simple code and this is definitely demonstrate it well.

I would also like to see some real life example of usage.

# May 23, 2008 12:12 AM

Gil Fink's Blog said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 1:13 PM