Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Learning about Adapter and Bridge Design Patterns in JAVA

In this article, we will study about the two main design patterns in JAVA which comes under the category of structural design patterns in JAVA: Adapter and Bridge Design pattern.

Structural design patterns are the type of design that simplifies the design by identifying a simple way to realize relationships between entities. They are of seven types such as Adapter Design Pattern, Bridge Design Pattern, Composite Design Pattern, Decorator Design Pattern, Facade Design Pattern, Flyweight Design Pattern and Proxy Design Pattern. In this article, we will discuss the first two types of design patterns in detail. Let us start with Adapter Design Patterns.

ADAPTER DESIGN PATTERNS

Adapter Design Pattern helps the user to connect two unrelated interfaces so that they can work together. So, the joining between those two interfaces is known as adapter. So, in simple terms it acts as a bridge between two incompatible interfaces. This type of pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. In order to have a better understanding, we will take an example. In this example, we will have an interface named as MRBOOLAudioVideoPlayer interface and a concrete class named as MRBOOLAudioPlayer implementing the MRBOOLAudioVdieoPlayer interface. This MRBOOLAudioPlayer can play mp3 format audio files by default. Now, there is another interface named as AdvancedMRBOOLAudioVideoPlayer and concrete classes implementing the MRBOOLAdvancedAudioVideoPlayer interface. These classes can play .avi and .mkv format files.

Now, in this example, we want to make MRBOOLAudioPlayer to play other formats as well. In order to do this, we will create an adapter class named as MRBOOLMediaAdapter which implements the MRBOOLAudioVideoPlayer interface and uses AdvancedMRBOOLAudioVideoPlayer objects to play the required format. So, in this case, MRBOOLAudioPlayer uses the adapter class MRBOOLMediaAdapter passing it the desired audio type without knowing the actual class which can play the desired format. So, let us start coding the above written thing. We have to follow a step by step approach. In the first step, we will create interfaces for MRBOOLAudioVideoPlayer and AdvancedMRBOOLAudioVideoPlayer.

Listing 1: Shows the coding for creating interfaces (for MRBOOLAudioVideoPlayer.java)

public interface MRBOOLAudioVideoPlayer {
   public void run(String audiovideoType, String fileName);
}

Listing 2: Shows the coding for creating interfaces (for AdvancedMRBOOLAudioVideoPlayer.java)

public interface AdvancedMRBOOLAudioVideoPlayer {	
   public void playAvi(String fileName);
   public void playMkv(String fileName);
}

Now, after creating interfaces, we need to create concrete classes which implement the AdvancedMRBOOLAudioVideoPlayer in the second step.

Listing 3: Shows the code for creating concrete classes implementing the AdvancedMRBOOLAudioVideoPlayer (for AviPlayer.java)

public class AviPlayer implements AdvancedMRBOOLAudioVideoPlayer{
   @Override
   public void playAvi(String fileName) {
      System.out.println("Playing avi file. Name: "+ fileName);		
   }

   @Override
   public void playMkv(String fileName) {
      //do nothing
   }
}

Listing 4: Shows the code for creating concrete classes implementing the AdvancedMRBOOLAudioVideoPlayer (for MkvPlayer.java)
public class MkvPlayer implements AdvancedMRBOOLAudioVideoPlayer{

   @Override
   public void playAvi(String fileName) {
      //do nothing
   }

   @Override
   public void playmkv(String fileName) {
      System.out.println("Playing mkv file. Name: "+ fileName);		
   }
}

Now, after this user needs to create adapter class which implements the MRBOOlAudioVideoPlayer interface.

Listing 5: Shows the code to create adapter class implementing the MRBOOLAudioVideoPlayer interface (for MRBOOLMediaAdapter.java)

public class MRBOOLMediaAdapter implements MRBOOLAudioVideoPlayer {

   AdvancedMRBOOLAudioVideoPlayer advancedMRBOOLMusicPlayer;

   public MRBOOLMediaAdapter(String audioType){
      if(audioType.equalsIgnoreCase("avi") ){
         advancedMRBOOLMusicPlayer = new AVIPlayer();			
      } else if (audioType.equalsIgnoreCase("Mkv")){
         advancedMRBOOLMusicPlayer = new MkvPlayer();
      }	
   }

   @Override
   public void play(String audioType, String fileName) {
      if(audioType.equalsIgnoreCase("mkv")){
         advancedMRBOOLMusicPlayer.playMKV(fileName);
      }else if(audioType.equalsIgnoreCase("Avi")){
         advancedMRBOOLMusicPlayer.playAvi(fileName);
      }
   }
}

Now, we will create concrete classes for implementing the MRBOOLAudioVideoPlayer interface.

Listing 6: Shows the coding for creating concrete class which implement the MRBOOLAudioVideoPlayer interface (for MRBOOLAudioPlayer.java)

public class MRBOOLAudioPlayer implements MRBOOLAudioVideoPlayer {
   MRBOOLMediaAdapter MRBOOLmediaAdapter; 

   @Override
   public void play(String audioType, String fileName) {		

      //inbuilt support to play mp3 music files
      if(audioType.equalsIgnoreCase("mp3")){
         System.out.println("Playing mp3 file. Name: "+ fileName);			
      } 
      //MRBOOLmediaAdapter is providing support to play other file formats
      else if(audioType.equalsIgnoreCase("avi") 
         || audioType.equalsIgnoreCase("mkv")){
         MRBOOLmediaAdapter = new MRBOOLMediaAdapter(audioType);
         MRBOOLmediaAdapter.play(audioType, fileName);
      }
      else{
         System.out.println("Invalid media. "+
            audioType + " format not supported");
      }
   }   
}

Now, after this, the next step is to use the MRBOOLAudioPlayer to play different types of format.

Listing 7: Shows the code for doing the above mentioned task (MRBOOLAdapterPattern.java)

public class MRBOOLAdapterPattern {
   public static void main(String[] args) {
      MRBOOLAudioPlayer MRBOOLaudioPlayer = new MRBOOLAudioPlayer();

      MRBOOL audioPlayer.play("mp3", "carnival of rust.mp3");
      MRBOOLaudioPlayer.play("avi", "tokyo drift.avi");
      MRBOOLaudioPlayer.play("mkv", "summer of 67.mkv");
      MRBOOLaudioPlayer.play("mp4", "traveller.avi");
    }
}

Now, the last step is to verify the output.

Listing 8: Shows the output of verify the Adapter Design Pattern use

Playing mp3 file. Name: carnival of rust.mp3
Playing avi file. Name: Tokyo drift.avi
Playing mkv file. Name: summer of 67.mkv
Invalid media. mp4 format not supported

BRIDGE DESIGN PATTERN

Now, we will study about Bridge design pattern in JAVA. This type of design pattern is used when the user wants to decouple an abstraction from its implementation so that the two can vary independently. This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other. Let us take an example to understand this in detail.

Listing 9: Shows the code for creating bridge implementer interface (for DrawDifferentColorCircles.java)

public interface DrawDifferentColorCircles {
   public void drawCircle(int diameter, int a, int b);
}

Listing 10: Shows the code for creating concrete bridge implementer classes for implementing the DrawDifferentColorCircles interface (for BlueCirlce.java)

public class BlueCircle implements DrawDifferentColorCircles {
   @Override
   public void drawCircle(int diameter, int a, int b) {
      System.out.println("Drawing Circle[ color: blue, diameter: "
         + diameter +", a: " +a+", "+ b +"]");
   }
}

Listing 11: Shows the code for creating concrete bridge implementer classes for implementing the DrawDifferentColorCircles interface (for GreyCirlce.java)

public class GreyCircle implements DrawDifferentColorCircles {
   @Override
   public void drawCircle(int diameter, int a, int b) {
      System.out.println("Drawing Circle[ color: grey, diameter: "
         + diameter +", a: " +b+", "+ b +"]");
   }
}

Listing 12: Shows the code for creating an abstract class Figure using the DrawDifferentColorCirles interface

public abstract class Figure {
   protected DrawDifferentColorCirles  drawdifferentcolorcirles ;
   protected Shape(DrawDifferentColorCirles  drawdifferentcolorcirles){
      this. drawdifferentcolorcirles  = drawdifferentcolorcirles ;
   }
   public abstract void draw();	
}

Listing 13: Shows the code for creating concrete class implementing the Figure interface

public class Circle extends Figure {
   private int a, b, diameter;

   public Circle(int a, int b, int diameter, DrawDifferentColorCircles drawdifferentcolorcircles) {
      super(drawdifferentcolorcircles);
      this.a = a;  
      this.b = b;  
      this.diameter = diameter;
   }

   public void draw() {
      drawdifferentcolorcircles.drawCircle(diameter,a,b);
   }
}

Finally, we can implement this code and verify the code.

Hope you like reading it!



I am well versed with Computer Programming languages and possess good working knowledge on software languages such as C, Java, PHP, HTML and CSS

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login