Monday, June 6, 2022

Java Tutorial - Bridge Pattern

Chapters

Bridge Pattern

Bridge Pattern is a design pattern that is meant to "decouple an abstraction from its implementation so that the two can vary independently". For example, we have shape and we want to implement a color feature. To do this, we create a class for shape and color; combine them in one hierarchy.

The problem with this is that these two classes are going to be tightly coupled and we, programmers, avoid tight coupling if possible. To avoid tight coupling in this case, we will decouple a class of color from class of shape.

Class of shape will stand as an abstraction for class of shape color. Then, an implementation for coloring shapes of shape class will be delegated to class of shape color. We will use this diagram as reference:
Diagram
Courtesy of Wikipedia

This example demonstrates bridge pattern.
/*
Client
*/

public class ClientCode{
  public static void main(String[] args){
    Shape circ = 
    new Circle(new StandardShapeColorStyle());
    
    circ.setColorStyle("STYLE1");
    System.out.println
    ("Color style of this shape:");
    System.out.println
    (circ.getShapeColorStyle());
  }
}

/*
Abstraction
*/
abstract class Shape{
  private ShapeColorStyle scs;
  private String colorStyle;
  
  Shape(ShapeColorStyle scs){
    this.scs = scs;
  }
  
  //implementation of setColorStyle of
  //Shape class delegated to setColorStyle
  //of ShapeColorStyle
  public void setColorStyle(String style){
    if(scs == null){
      System.err.println
      ("ColorStyle instance not defined!");
      return;
    }
    colorStyle = scs.setColorStyle(style);
  }
  
  String getShapeColorStyle(){
    return colorStyle;
  }
}

class Circle extends Shape{

  Circle(ShapeColorStyle scs){
    super(scs);
  }
  
  //refined version of setColorStyle
  //of Shape class. I find this one
  //optional
  @Override
  public void setColorStyle(String style){
    System.out.println
    ("This shape is circle.");
    System.out.println
    ("Circle has infinite vertices.");
    System.out.println
    ("Setting color style...");
    super.setColorStyle(style);
  }
  
}

/*
Implementor
*/

interface ShapeColorStyle{
  String setColorStyle(String style);
}

class StandardShapeColorStyle implements 
                              ShapeColorStyle{
  private final String STYLE1 = "STYLE1";
  private final String STYLE2 = "STYLE2";
  
  @Override
  public String setColorStyle(String style){
    String colorStyle = null;
    
    switch(style){
      case STYLE1:
      colorStyle = "Color Style One";
      break;
      
      case STYLE2:
      colorStyle = "Color Style Two";
      break;
    }
    return colorStyle;
  }
}

Result
This shape is circle.
Circle has infinite vertices
Setting color style...
Color style of this shape:
Color Style One
One of the advantages of bridge pattern is that we can independently expand two related hierarchies. In the example above, Shape and ShapeColorStyle classes can be expanded without affecting each other.

No comments:

Post a Comment