Java
Usage examples: The Bridge pattern is especially useful when dealing with cross-platform apps, supporting multiple types of database servers or working with several API providers of a certain kind (for example, cloud platforms, social networks, etc.)
Identification: Bridge can be recognized by a clear distinction between some controlling entity and several different platforms that it relies on.
Bridge between devices and remote controls
This example shows separation between the classes of remotes and devices that they control.
Remotes act as abstractions, and devices are their implementations. Thanks to the common interfaces, the same remotes can work with different devices and vice versa.
The Bridge pattern allows changing or even creating new classes without touching the code of the opposite hierarchy.
devices
devices/Device.java: Common interface of all devices
package refactoring_guru.bridge.example.devices;
public interface Device {
boolean isEnabled();
void enable();
void disable();
int getVolume();
void setVolume(int percent);
int getChannel();
void setChannel(int channel);
void printStatus();
}devices/Radio.java: Radio
devices/Tv.java: TV
remotes
remotes/Remote.java: Common interface for all remotes
remotes/BasicRemote.java: Basic remote control
remotes/AdvancedRemote.java: Advanced remote control
Demo.java: Client code
OutputDemo.txt: Execution result
Last updated