Bridge - Structural Design Pattern
2019, Feb 11
🚡 Bridge
Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
Decouple an abstraction from its implementation so that the two can vary independently.
Example:
interface WebPage {
public void setTheme(Theme theme);
public String getContent();
}
class AboutPage implements WebPage {
protected Theme theme;
public void setTheme(Theme theme) {
this.theme = theme;
}
public String getContent() {
return "About page in "+ this.theme.getColor();
}
}
class CareersPage implements WebPage {
protected Theme theme;
public void setTheme(Theme theme) {
this.theme = theme;
}
public String getContent() {
return "Careers page in "+ this.theme.getColor();
}
}
interface Theme {
public Color getColor();
}
class DarkTheme implements Theme {
public Color getColor() {
return Color.DarkBlack;
}
}
class LightTheme implements Theme {
public Color getColor() {
return Color.OffWhite;
}
}
class AquaTheme implements Theme{
public Color getColor() {
return Color.LightBlue;
}
}
Theme darkTheme = new DarkTheme();
AboutPage about = new AboutPage(darkTheme);
CareersPage careers = new CareersPage(darkTheme);
When to use? When we want to decouple an abstraction from its implementation so that the two can vary independently.