Bridge
Last updated
Was this helpful?
Last updated
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.
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.
Here is an example how to read MS Word using Bridge pattern.