Bridge Pattern
This is an example for implementation for Bridge pattern
Concept
Use the Bridge Pattern to vary not only your implementations but also your abstractions.
the next picture ilustrase easy the main idea.
when you have Abstraction and implementation(in the future it becomes in another abstraction), we can face we are creating a cartesian product, which is hell because it increments exponentially the code and That’s a very common issue with class inheritance, then came the bridge pattern for saving the sake, for avoiding that, the bridge pattern specifies the dependency implementation switch the inheritance for object composition, it allows changes on the implementation and the abstraction regardless
for example
Note: this is an example of bridge patterns, its will be a series for implement all patterns in java / python / go
in the next classes you can see the implementation for apply the bridge pattern and how in the MainClass call all implemantation for show usage, you can see the flexibility for use this structure in the problem for OOP
abstraccions
public abstract class Shape {
protected Color color;
protected Shape(Color color) {
this.color = Color;
}
abstract public void applyColor();
}
public abstract class Color {
private String color;
protected Color(String Color) {
this.color = Color;
}
public void applyColor();
//getters
//seters
}
Usage
public class Triangle extends Shape{
public Triangle(Color c) {
super(c);
}
@Override
public void applyColor() {
System.out.print("Triangle filled with color ");
color.applyColor();
}
}
public class Pentagon extends Shape{
public Pentagon(Color c) {
super(c);
}
@Override
public void applyColor() {
System.out.print("Pentagon filled with color ");
color.applyColor();
}
}
public class RedColor implements Color{
public void applyColor(){
System.out.println("red.");
}
}
public class GreenColor implements Color{
public void applyColor(){
System.out.println("green.");
}
}
public class MainClass {
public static void main(String[] args){
Shape tri = new Triangle(new RedColor());
tri.applyColor();
Shape pent = new Pentagon(new GreenColor());
pent.applyColor();
}
}