Tutorials  /  JavaScript

The Art of Decoupling: The Bridge Design Pattern in Java

Ccentron Redaktion · October 2024 ·3 min read ·JavaScript, Tutorial

Discover the Bridge Design Pattern in Java, a method to efficiently decouple abstractions and implementations. This design pattern helps you achieve flexibility in your code by allowing both the abstraction and the implementation to evolve independently. Learn how to achieve design flexibility through composition instead of inheritance, a key principle of clean software design. This pattern is especially useful for developers who want to create clean, maintainable, and extensible codebases in large or growing projects.

What is the Bridge Design Pattern?

The Bridge Design Pattern follows the principle of favoring composition over inheritance. It decouples an abstraction from its implementation so that the two can vary independently. This flexibility makes the pattern highly suitable for complex systems where you might want to change the abstraction or the implementation without affecting the other. The Bridge Pattern allows you to organize your code efficiently and reduce the dependency between different parts of your application, making your system easier to maintain in the long run.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Example of the Bridge Design Pattern in Java

To better understand the Bridge Design Pattern, let's look at an example with interface hierarchies for both interfaces and implementations. The implementation of the Bridge Design Pattern is done using composition, as demonstrated in the following Java code:

Go
package com.journaldev.design.bridge;

public interface Color {
    void applyColor();
}

package com.journaldev.design.bridge;

public abstract class Shape {
    protected Color color;
    
    public Shape(Color c){
        this.color = c;
    }
    
    abstract public void applyColor();
}

For the concrete implementation classes like Triangle and Pentagon, the code looks as follows:

Go
package com.journaldev.design.bridge;

public class Triangle extends Shape {

    public Triangle(Color c) {
        super(c);
    }

    @Override
    public void applyColor() {
        System.out.print("Triangle filled with color ");
        color.applyColor();
    } 

}

package com.journaldev.design.bridge;

public class Pentagon extends Shape {

    public Pentagon(Color c) {
        super(c);
    }

    @Override
    public void applyColor() {
        System.out.print("Pentagon filled with color ");
        color.applyColor();
    } 

}

The implementation classes for RedColor and GreenColor look like this:

Go
package com.journaldev.design.bridge;

public class RedColor implements Color{

    public void applyColor(){
        System.out.println("red.");
    }
}

package com.journaldev.design.bridge;

public class GreenColor implements Color{

    public void applyColor(){
        System.out.println("green.");
    }
}

A test program demonstrates the application of the Bridge Pattern:

Go
package com.journaldev.design.test;

import com.journaldev.design.bridge.GreenColor;
import com.journaldev.design.bridge.Pentagon;
import com.journaldev.design.bridge.RedColor;
import com.journaldev.design.bridge.Shape;
import com.journaldev.design.bridge.Triangle;

public class BridgePatternTest {

    public static void main(String[] args) {
        Shape tri = new Triangle(new RedColor());
        tri.applyColor();
        
        Shape pent = new Pentagon(new GreenColor());
        pent.applyColor();
    }

}

The output of this test program demonstrates the functionality of the Bridge Pattern:

Code
Triangle filled with color red.
Pentagon filled with color green.

The Bridge Design Pattern is particularly useful when both the abstraction and the implementation can have independent hierarchies and when implementation details need to be hidden from the client application. It promotes a clean separation of responsibilities and facilitates maintenance and extension of the code. This decoupling ensures that you can adapt either side of the design independently, providing better scalability and organization for long-term project success.

You might also be interested in:

Bootstrap Sampling: An Introduction to Python

Development and Testing of Web Services with Axis2: A Practical Handbook

Efficient Database Interaction with Java: CallableStatement for Stored Procedures

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren