Name: Bridge Design Pattern
Type: Structural Design Pattern
Purpose: Decouple abstraction from implementation
(In other words, using composite over inheritance)
Sample Problem and Solution:
Type: Structural Design Pattern
Purpose: Decouple abstraction from implementation
(In other words, using composite over inheritance)
Sample Problem and Solution:
Assume that you are required to create some Shapes with two different patterns. Some sample shapes are SolidColorCircle, GradientCircle, SolidColorSquare, and GradientSquare. A possible design is provided below.
public interface Shape {
public void draw();
}
public abstract class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Create a circle with radius: " + radius);
}
}
public class SolidColorCircle extends Circle {
private String color;
public SolidColorCircle(double radius, String color) {
super(radius);
this.color = color;
}
@Override
public void draw() {
super.draw();
System.out.println("Fill with: " + color);
}
}
public class GradientCircle extends Circle {
private String colorX;
private String colorY;
public SolidColorCircle(double radius, String colorX, String colorY) {
super(radius);
this.colorX = colorX;
this.colorY = colorY;
}
@Override
public void draw() {
super.draw();
System.out.println("Fill with: " + colorX + " & " + colorY);
}
}
Square, SolidColorSquare, and GradientSquare are provided in GitHub.