Bridge Pattern
Photo by Modestas Urbonas on Unsplash

Bridge Pattern

Ever felt overwhelmed by the explosion of subclasses when trying to support multiple variations in your code?

What if you could mix and match different functionalities in your software without creating a tangled mess of code?

Incomes the Bridge Pattern

THE WHAT

The Bridge pattern is a structural design pattern that allows you to split a large class or a set of closely related classes into two separate hierarchies: abstraction and implementation. These two hierarchies can be developed independently, making the code more manageable and extensible.

The General Problem It Tries to Solve

The main problem the Bridge pattern addresses is the complexity that arises when extending a class in multiple dimensions. This situation often occurs in large systems where classes need to support multiple variants of functionality, such as different shapes and colors, or various graphical user interfaces (GUIs) and operating systems (APIs).

Example of the General Problem

Consider a scenario where we need to create various types of documents (like Reports and Letters) in different formats (like PDF and Word). If we try to use inheritance to cover all combinations, we end up with classes like PDFReport, WordReport, PDFLetter, WordLetter, and so on. This approach quickly becomes unmanageable as the number of combinations increases.

Generalizing the When

Use the Bridge pattern when you need to extend a class in multiple independent dimensions. This pattern is particularly useful when you want to divide and organize a monolithic class that has several variants of some functionality. It’s also beneficial when you need to switch implementations at runtime.

THE HOW

Here’s how you can implement the Bridge pattern using the example of documents and formats in Java:

Step 1: Identify the orthogonal dimensions in your classes.

In this case, the dimensions are Document (abstraction) and Format (implementation).

Step 2: Define the operations the client needs in the base abstraction class.

// Abstraction
abstract class Document {
    protected Format format;

    public Document(Format format) {
        this.format = format;
    }

    abstract void create();
}        

Step 3: Determine the operations available on all platforms. This involves defining the methods that the implementation classes must provide, regardless of their specific details.

// Implementation
interface Format {
    void generate(String content);
}        

Step 4: Create concrete implementation classes for all platforms.

// Concrete Implementations
class PDFFormat implements Format {
    @Override
    public void generate(String content) {
        System.out.println("Generating PDF with content: " + content);
    }
}

class WordFormat implements Format {
    @Override
    public void generate(String content) {
        System.out.println("Generating Word document with content: " + content);
    }
}        

Step 5: Add a reference field for the implementation type inside the abstraction class.

// Refined Abstraction
class Report extends Document {
    public Report(Format format) {
        super(format);
    }

    @Override
    void create() {
        System.out.print("Creating Report. ");
        format.generate("Report Content");
    }
}

class Letter extends Document {
    public Letter(Format format) {
        super(format);
    }

    @Override
    void create() {
        System.out.print("Creating Letter. ");
        format.generate("Letter Content");
    }
}        

Step 6: Pass an implementation object to the abstraction’s constructor to associate one with the other.

// Client
public class BridgePatternExample {
    public static void main(String[] args) {
        Document pdfReport = new Report(new PDFFormat());
        pdfReport.create();

        Document wordLetter = new Letter(new WordFormat());
        wordLetter.create();
    }
}        

Pros:

  • Flexibility: You can mix and match different document types with different formats without creating a large number of subclasses.
  • Decoupling: The abstraction (Document) and implementation (Format) can evolve independently.
  • Extensibility: New document types and formats can be added without modifying existing code.

Cons:

  • Overhead: There might be a slight performance overhead due to the increased number of objects and the delegation of work.

Thank you

要查看或添加评论,请登录

Chirag Vaswani的更多文章

  • Iterator Pattern

    Iterator Pattern

    WHAT IS ITERATOR PATTERN? Iterator pattern allows us to traverse elements of a collection without exposing its…

  • Command Pattern

    Command Pattern

    The Command design pattern encapsulates a request as an object, thereby allowing users to parameterize clients with…

  • Chain of Responsibility Pattern

    Chain of Responsibility Pattern

    Ever faced challenges managing complex workflows with multiple handlers in a serial manner? CoR to the rescue… THE WHAT…

  • Proxy Pattern

    Proxy Pattern

    Ever wondered how you can manage heavy resources in your applications without slowing down performance? There is way to…

  • Flyweight Pattern

    Flyweight Pattern

    Flyweight is a class in boxing which includes fighters weighing up to and including 51 kg (112 lb) for a title fight…

  • Facade Pattern

    Facade Pattern

    The What The Facade Pattern is a structural design pattern that provides a simplified interface to a complex subsystem,…

  • Decorator Pattern

    Decorator Pattern

    The Decorator Pattern is a structural design pattern that allows you to dynamically attach additional responsibilities…

  • Composite Pattern

    Composite Pattern

    Have you ever struggled with managing complex hierarchical structures in your code? Composite pattern can simplify your…

  • Adaptor Pattern

    Adaptor Pattern

    The Adapter Pattern is about creating an intermediary abstraction that translates, or adapts, one interface into…

  • Singleton Pattern: A Comprehensive Guide

    Singleton Pattern: A Comprehensive Guide

    The Singleton Pattern is one of the most widely used design patterns in software engineering. Whether you are working…