Click here to Skip to main content
15,868,141 members
Articles / Binding
Article

Bridge Pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 6.5K   1  
The Bridge PatternThe bridge pattern decouples an abstraction from its implementation so the two can vary independently. In other words we make a

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

The Bridge Pattern
The 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.


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 Abstraction

 

    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 Operation()

        {

            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, 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.

This article was originally posted at http://wiki.asp.net/page.aspx/489/bridge-pattern

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --