It is used to separate the interface from its implementation and enables us to change either interface or implementation easily. Generally it is used to keep the client interface constant and even if we change the implementation.
It seems quite similar to adapter pattern, where we can adapt the functionalities of any other class and expose this with a separate interface. But the difference is in purpose, Adapter is implemented to use the existing implementation with a new interface but Bridge is used to separate the implementation from interface to user so that user won’t be affected by any change in implementation.
- We want to present some data to user. Based on the user profile, the data can be either in simple list form having names only or in a table having complete details. Here we can implement the logic to fetch the data and arranging it in required format in a separate class which can be accessed from user interface which could be a list, table or tree.
When to use
- When implementation of functionality can be used by multiple interfaces and we don’t want to duplicate the code for each interface.
- When we want to keep the user interface intact from any change in implementation.
- Avoid change in interface on any change in implementation
- Help to avoid the duplicate code for different interfaces having similar kind of back end requirements.
- It can prevent us to recompile the user interface on any change in implementation, but only bridge and implementation can be recompiled.