Bridge Pattern

 Rate It (3)

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.

Revision number 3, Wednesday, September 23, 2009 5:01:52 PM by TheNephalim

Comments

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Prototype Pattern

The prototype is built upon the use of object cloning. The prototype creates new objects by cloning one of its concrete classes. The prototype is used in the following situations: You need to hide the concrete product classes from the client. You want to reduce

Threat Modeling

It's absolutely necessary if you're serious about security. Whitepapers/Books/Blogs Threat Modeling for ASP.NET (PDF) - an excellent white paper from Rüdiger Grimm and Henrik Eichstädt from the University of Kent Threat Modeling book from MSPress

IDisposable Pattern

The IDisposable pattern isn't one of the a classic patterns. It's a pattern suggested in MSDN to implement the IDisposable interface. You should be familiar with the pattern or with the interface because it's a basic thing to know about the .Net

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (14)
  2. codehard (3)
  3. Babunareshnarra (2)
  4. Dungimon (1)
  5. cabhilash (1)
Microsoft Communities