A Bridge Pattern says that just “decouple the functional abstraction from the implementation so that the two can vary independently”.
The Bridge Pattern is also known as Handle or Body.
Advantage of Bridge Pattern
- It enables the separation of implementation from the interface.
- It improves the extensibility.
- It allows the hiding of implementation details from the client.
Usage of Bridge Pattern
- When you don’t want a permanent binding between the functional abstraction and its implementation.
- When both the functional abstraction and its implementation need to extended using sub-classes.
- It is mostly used in those places where changes are made in the implementation does not affect the clients.
Example of Bridge Pattern
The UML given below describes the example of bridge pattern.
UML for Bridge Pattern:
Implementation of above UML:
Create a Question interface that provides the navigation from one question to another or vice-versa.
Create a JavaQuestions implementation class that will implement Question interface.
Create a QuestionManager class that will use Question interface which will act as a bridge..
Create a QuestionFormat class that will extend the QuestionManager class
Create a BridgePatternDemo class.