Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Structural Patterns

Bridge

PreviousFacadeNextDecorator

Last updated 5 years ago

Was this helpful?

  • Decouple an abstraction from its implementation so that the two can vary independently.

  • Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy

  • Beyond encapsulation, to insulation (object wrapping)

At first sight, the Bridge pattern looks a lot like the Adapter pattern in that a class is used to convert one kind of interface to another. However, the intent of the Adapter pattern is to make one or more classes' interfaces look the same as that of a particular class. The Bridge pattern is designed to separate a class's interface from its implementation so you can vary or replace the implementation without changing the client code.

Example - Document reader

We are going to create document parser that is responsible to read file and return its content as text. Implementation of DocumentParser will be different for MS Word, PDF and so on. Instead of using implementations of DocumentParser interface, we want to create DocumentReader because it will provide API for reading files.

interface DocumentParser {
    String parse();
}

class WordParser implements DocumentParser {

    private String fileName;

    public WordParser(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public String parse() {
        return "Content of Word document.";
    }
}

interface DocumentReader {
    String getText();
}

class DocumentReaderImpl implements DocumentReader {

    private DocumentParser documentParser;

    public DocumentReaderImpl(DocumentParser documentParser) {
        this.documentParser = documentParser;
    }

    @Override
    public String getText() {
        // TODO: here we would do more stuff if needed... 
        return documentParser.parse();
    }
}

Here is an example how to read MS Word using Bridge pattern.

DocumentReader reader = new DocumentReaderImpl(new WordParser("document.docx"));
String text = reader.getText();
more