Difference between Bridge pattern and Adapter pattern in java

By | March 23, 2023

Both the Bridge pattern and Adapter pattern are used to decouple an interface from its implementation. However, they differ in the way they achieve this decoupling.

The Bridge pattern is used when you want to separate an abstraction (an interface) from its implementation, so that the two can vary independently. It allows you to change the implementation of an abstraction without affecting the clients that use it. The Bridge pattern is useful when you have multiple implementations of an interface and you want to switch between them at runtime.

The Adapter pattern is used when you want to convert one interface into another, so that two incompatible interfaces can work together. It allows you to wrap an existing class with a new interface, without modifying the existing class. The Adapter pattern is useful when you have a class that is already implemented, but it doesn’t conform to the interface that you need.

Here’s an example of the Bridge pattern in Java:

Suppose you have an interface called Shape that represents geometric shapes, and two implementations of this interface called Circle and Rectangle. You also have an interface called DrawingAPI that represents the way in which the shapes are drawn, and two implementations of this interface called DrawingAPI1 and DrawingAPI2.

To separate the abstraction of the Shape interface from its implementation of Circle and Rectangle, you can create a separate interface called ShapeAPI that represents the way in which the shapes are drawn. This interface will be implemented by both Circle and Rectangle.

public interface ShapeAPI {
public void draw();
}

public class Circle implements Shape {
private DrawingAPI drawingAPI;
public Circle(DrawingAPI drawingAPI) {
this.drawingAPI = drawingAPI;
}
public void draw() {
drawingAPI.drawCircle();
}
}

public class Rectangle implements Shape {
private DrawingAPI drawingAPI;
public Rectangle(DrawingAPI drawingAPI) {
this.drawingAPI = drawingAPI;
}
public void draw() {
drawingAPI.drawRectangle();
}
}

public interface DrawingAPI {
public void drawCircle();
public void drawRectangle();
}

public class DrawingAPI1 implements DrawingAPI {
public void drawCircle() {
System.out.println(“Drawing Circle using API1”);
}
public void drawRectangle() {
System.out.println(“Drawing Rectangle using API1”);
}
}

public class DrawingAPI2 implements DrawingAPI {
public void drawCircle() {
System.out.println(“Drawing Circle using API2”);
}
public void drawRectangle() {
System.out.println(“Drawing Rectangle using API2”);
}
}

Here’s an example of the Adapter pattern in Java:

Suppose you have an interface called MediaPlayer that represents a media player, and an implementation of this interface called AudioPlayer that can play only MP3 files. You also have another interface called AdvancedMediaPlayer that represents a media player that can play other types of files such as VLC and MP4, and two implementations of this interface called VlcPlayer and Mp4Player.

To make AudioPlayer play other types of files such as VLC and MP4, you can create an adapter class called MediaAdapter that implements MediaPlayer interface and uses AdvancedMediaPlayer implementations to play the different file types.

public interface MediaPlayer {
public void play(String audioType, String fileName);
}

public class AudioPlayer implements MediaPlayer {
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase(“mp3”)) {
System.out.println(“Playing MP3 file: ” + fileName);
}
else {
System.out.println(“Invalid media. ” + audioType + ” format not supported.”);
}

Leave a Reply

Your email address will not be published. Required fields are marked *