Bridge Pattern
Is like Adapter Design Pattern but focus to the future
- Decouple Abstraction and implementation
- Decouple abstraction from implementation
- Changes in abstraction don't affect the client
- Ex. JDBC
Example:
- Shape class is the abstraction
- Execute the implementation of the interface
- Color is the interface
- The interface will be executed by the class that extends from Shape
public class Shape2BridgeDemo {
public static void main(String args[]){
Color blue = new Red();
Shape square = new Square(blue);
square.applyColor();
}
}
----------------------------------------------------------
abstract class Shape {
protected Color color;
public Shape(Color color){
this.color = color;
}
abstract public void applyColor();
}
--------------------------------------------------------------
public class Square extends Shape {
public Square(Color color) {
super(color);
}
@Override
public void applyColor() {
color.applyColor();
}
}
------------------------------------------------------------------
public interface Color {
public void applyColor();
}
------------------------------------------------------------------
public class Red implements Color {
public Red() {
}
@Override
public void applyColor() {
System.out.println("Applying color: red");
}
}
Comentarios
Publicar un comentario