Click here to Skip to main content
Licence CPOL
First Posted 19 Apr 2011
Views 8,417
Bookmarked 20 times

Bridge Design Pattern

By | 4 May 2011 | Technical Blog
Bridge Design Pattern Clearly Explained with Example
A Technical Blog article. View original blog here.[^]

You can see this and other great articles on design patterns here.  

The bridge design pattern allows you to separate the abstraction from the implementation. In the bridge pattern, there are 2 parts - the first part is the Abstraction, and the second part is the Implementation. The bridge pattern allows the Abstraction and the Implementation to be developed independently, and the client code can access only the Abstraction part without being concerned about the Implementation part.

Let's look at an example to see the concept behind the bridge pattern. For example, inside a house, there are appliances that you can turn on or off, such as the floor lamp, the TV, and the vacuum cleaner. There are different ways to turn the appliance on or off, such as using the on/off switch, the pull switch, or using a remote control. The concept of turning the appliance on or off is the Abstraction part in the bridge pattern, and the user only needs to know the Abstraction part. This is the first part of the bridge pattern.

The left side is the Abstraction part with the following classes:

  • Abstraction is the abstract parent class of the ConcreteAbstraction class. It defines the Function method for the client code to call. The protected implementor variable holds the reference to the object that performs the implementation.
  • ConcreteAbstraction is the concrete class that is inherited from the Abstraction class.

The right side is the Implementation part with the following classes:

  • IImplementor is the interface that all the implementation classes must implement.
  • ConcreteImplementor is the concrete class that performs the implementation.

Applying this UML to our example means that the left side represents controls that can turn appliances on or off, such as the on/off switch, the pull switch, or the remote control, while the right side represents the actual appliances that performs the action, such as the TV or the VacuumCleaner.  

Therefore, the UML of our example will be:

Another example of the bridge pattern is the copy and paste function in many applications. The copy and paste function is the abstraction, where the user only needs to know how to use it, and the actual action of transferring the information into the memory and transferring the information onto the application is the implementation.

The key benefit of the bridge design pattern is that it allows you to develop the Abstraction and the Implementation parts independently. It also cuts down on the number of classes that you need to create to fulfill all the possible combinations of the Abstractions (user interface concepts) and the Implementations (actual actions behind the scene).

 

Below are the implementation code and the output using the example given. Notice that the client code uses only the Abstraction part to perform the actions, and you can develop the Abstraction and the Implementation parts independently:

    class Program
    {
        static void Main(string[] args)
        {
            IAppliance tv = new TV("Bedroom TV");  //implementation object
            IAppliance vacuum = new VaccumCleaner
			("My Vacuum Cleaner");  //implementation object

            Switch s1 = GetSwitch(tv);  //convert to abstraction
            Switch s2 = GetSwitch(vaccum);  //convert to abstraction

            //*** client code works only with the abstraction objects, 
	   //not the implementation objects ***
            s1.TurnOn();
            s2.TurnOn();
        }

        //convert implementation object to abstraction object
        static Switch GetSwitch(IAppliance a)
        {
            return new RemoteControl(a);
        }
    }

    //the abstraction
    public abstract class Switch
    {
        protected IAppliance appliance;
        public abstract void TurnOn();
    }

    //the implementation
    public interface IAppliance
    {
        void Run();
    }

    //concrete abstraction
    public class RemoteControl : Switch
    {
        public RemoteControl(IAppliance i)
        {
            this.appliance = i;
        }

        public override void TurnOn()
        {
            appliance.Run();
        }
    }

    //concrete implementation
    public class TV : IAppliance
    {
        private string name;

        public TV(string name)
        {
            this.name = name;
        }

        void IAppliance.Run()
        {
            Console.WriteLine(this.name + " is running");
        }
    }

    //concrete implementation
    public class VaccumCleaner : IAppliance
    {
        private string name;

        public VaccumCleaner(string name)
        {
            this.name = name;
        }

        void IAppliance.Run()
        {
            Console.WriteLine(this.name + " is running");
        }
    }

Liked this article? You can see this and other great articles on design patterns here.  

License

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

About the Author

DevLake



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 2] hi Pinmembercomp science10:28 24 Apr '11  
GeneralWTL? PinmemberNemanja Trifunovic4:40 22 Apr '11  
GeneralRe: WTL? PinsubeditorIndivara5:00 23 Apr '11  
GeneralRe: WTL? PinmemberCodingBruce5:13 23 Apr '11  
GeneralMy vote of 3 Pinmemberervegter3:36 22 Apr '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid