Design Pattern In Java Part XVII Bridge Design Pattern

Posted By : Aftab Alam | 31-Oct-2018

1. As I have written last blog on Template Method Design and this blog is going to be on Bridge Design Pattern is meant to decouple an abstraction from its implementation so that they can grow independently.

     1.1. Purpose of Design Pattern:- The purpose of design pattern, as per Gang of Four(GoF), to solve recurring problems to design flexible and reusable object-oriented Softwares.

     1.2. Structural Design Pattern:- In Software Engineering, the structural design pattern is those design pattern which makes design easy by identifying a way to realize the relationship between entities.

     1.3. Bridge Design Pattern:- The Bridge Design Pattern comes under the category of structural design pattern and It is meant to decouple an abstration from its implementation so that two can grow independently. It uses encapsulation, aggregation and can use inheritance to separate responsibility into different classes.

2. Requirements:- To executed this program, pre-requisites have been listed below out.

    2.1. Ubuntu 14.04 LTS

    2.2. Ubuntu gedit editor

    2.3. java version "1.8.0_181" 

3. Implementation:- It has been divided into multiple steps.

   3.1. Code Snippet:- The code snippets have been provided below for two abstract classes and five concrete classes.

        3.1.1. The EntermaintDevice class is created as an abstract class which consists of two abstract methods, would be implemented by sub-classes and one non-abstract method.

      public abstract class EntertainmentDevice{

	public int deviceState;
	public int maxSetting;
	public int volumeLevel=0;

	public abstract void buttonFivePressed();
	public abstract void buttonSixPressed();
 	public void deviceFeedBack(){
		if(deviceState<maxSetting || deviceState<0){
			deviceState=0;
		}	
		System.out.println("On "+deviceState);
	}
  }

    3.1.2. The two concrete classes, TVDevice and DVDDevice, have been created from EntertainmentDevice class and they have implemented both the abstract method as per their need. They have implemented these methods in reverse of each other.

     public class TVDevice extends EntertainmentDevice {

	public TVDevice(int newDeviceState, int newMaxSetting){
		deviceState=newDeviceState;
		maxSetting=newMaxSetting;
	}

	public void buttonFivePressed(){
		System.out.println("Channel Down");
		deviceState--;
	}
	
	public void buttonSixPressed(){
		System.out.println("Channel Up");
		deviceState++;
	}
    }

 

    public class DVDDevice extends EntertainmentDevice {

	public DVDDevice(int newDeviceState, int newMaxSetting){
		deviceState=newDeviceState;
		maxSetting=newMaxSetting;
	}

	public void buttonFivePressed(){
		System.out.println("Channel Up");
		deviceState++;
	}
	
	public void buttonSixPressed(){
		System.out.println("Channel Down");
		deviceState--;
	}
   }

    3.1.3. The Remote class is created as abstract which has an abstract method and some non-abstract methods. An abstract method would be implemented by remote's sub-classes and non-abstract methods simply call methods of a class for which an instance is passed to remote's sub-class constructor. In this example, four classes have been derived from Remote class. 

    public class TVRemoteMute extends Remote {
	
	public TVRemoteMute(EntertainmentDevice theDevice){
		super(theDevice);
	}

	public void buttonNinePressed(){
		System.out.println("TV is muted");
	}
   }

     

   public class TVRemotePause extends Remote {
	
	public TVRemotePause(EntertainmentDevice theDevice){
		super(theDevice);
	}

	public void buttonNinePressed(){
		System.out.println("TV is paused");
	}
   }

     

   public class DVDRemotePause extends Remote {
	
	public DVDRemotePause(EntertainmentDevice theDevice){
		super(theDevice);
	}

	public void buttonNinePressed(){
		System.out.println("DVD is paused");
	}
   }

 

   public class DVDRemoteMute extends Remote {
	
	public DVDRemoteMute(EntertainmentDevice theDevice){
		super(theDevice);
	}

	public void buttonNinePressed(){
		System.out.println("DVD is muted");
	}
   }
  

    3.2.4. The BridgeDesignPattern class demonstrates the working of Bridge Design pattern.

     public class BridgeDesignPattern {

	public static void main(String a[]){
		
		TVRemoteMute theTVRemoteMute=new TVRemoteMute(new TVDevice(1, 200));
		System.out.println("\nTest Muted TV");
		theTVRemoteMute.buttonFivePressed();
		theTVRemoteMute.buttonSixPressed();
		theTVRemoteMute.buttonNinePressed();
		TVRemotePause theTVRemotePause=new TVRemotePause(new TVDevice(1, 200));
		System.out.println("\nTest Paused TV");
		theTVRemotePause.buttonFivePressed();
		theTVRemotePause.buttonSixPressed();
		theTVRemotePause.buttonNinePressed();

		DVDRemoteMute theDVDRemoteMute=new DVDRemoteMute(new DVDDevice(1, 200));
		System.out.println("\nTest Muted DVD");
		theDVDRemoteMute.buttonFivePressed();
		theDVDRemoteMute.buttonSixPressed();
		theDVDRemoteMute.buttonNinePressed();
		DVDRemotePause theDVDRemotePause=new DVDRemotePause(new DVDDevice(1, 200));
		System.out.println("\nTest Paused DVD");
		theDVDRemotePause.buttonFivePressed();
		theDVDRemotePause.buttonSixPressed();
		theDVDRemotePause.buttonNinePressed();
	}
    }

   3.2. Flow Diagram:- Listed below image is trying to depict Bridge Design Pattern flow.

Flow Diagram

   3.3. Commands:- To compile and execute this program, listed below commands are used on ubuntu terminal.

     3.3.1. "javac BridgeDesignPattern.java" to compile the java program

     3.3.2. "java BridgeDesignPattern" to execute the program which produces the result shown in below screenshot.

   3.4. Output:- When the above program is executed then It produces the output which is shown in the screenshot.

Output

 

 

 

 

 

 

 

 

 

3.5. References:- Listed below out are some references.

3.5.1. https://en.wikipedia.org/wiki/Bridge_pattern
3.5.2. https://www.youtube.com/watch?v=9jIgSsIfh_8&t=621s 

4. Conclusion:- The Bridge Design Pattern provides a facility to grow abstraction from its implementation grows independently which makes development easy in some real time scenario.

About Author

Author Image
Aftab Alam

Aftab has worked on multiple technologies in front-end as well as in back-end.

Request for Proposal

Name is required

Comment is required

Sending message..