The Implementation of Bridge Design Pattern

Posted By : Mohit Jain | 19-Aug-2018

 Implementation of this design pattern follows the modified version of the notion(idea) to prefer Aggregation(Has-A) over Inheritance(IS-A).

 

Objective:-

1.)Decouple implementation from interface/Abstraction.

2.) Hiding implementation details from Client is the essense of bridge design pattern.

 

Elements required  to implement this patteren are as follows:-

1.)Abstraction:- Abstraction Has-A Implementor reference which is the core of this design patteren . 

2.)Refined Abstraction:- Extends Abstaration and provide finer or one low level abstraction which is hidden from Implementer

3.)Implementer:- Defines basic operations having higher level than Abstraction .Totally Independent from Abstraction.

4.)Concrete Implementer:- Provide Concrete implementation of the Implementor.

 

Conclusion:- Abstraction can varies on its own but HAS-A Implementer involving in its finer level abstarction.Implementer is independent having concrete implementations ,so any other Abstraction can also use the same Implementer's implementation in its abstarction.We can also say that Abstaraction provides varient type of INPUT and Implementer takes this INPUT and Process this with concrete implementation logic to produce final OUTPUT .

 

Note:-Abstarction and Implementer can be abstract class or an interface.Input type can vary on its own and Implementer can process any varient type of Input with multiple implementation logic

 

Step 1:- Create Independent Implemeneter called "Color" which is used to color any type of Shape(ex. circle,sqaure with any concrete color).

public interface Color {

public void colorShape(String shapeType);

}

Step 2:- Create implentations of the above Implementer as "RedColor","GreenColor"

public class RedColor implements Color {

@Override public void colorShape(String shapeType) {

System.out.println("Drawing shape is"+shapeType+"With Red color"); }

}

public class GreenColor implements Color {
    @Override
    public void colorShape(String shapeType) {
        System.out.println("Drawing shape is"+shapeType+"With Green color");
    }
}

Step 3:- Create Abstarction called "Shape" which HAS-A Color as reference

public abstract class Shape {
    protected Color color;

    protected Shape(Color color){
        this.color = color;
    }
    public abstract void fillColor();
}

Step4:- Refined Abstraction of "Shape" as "Circle" ,"Square"

public class Circle extends Shape { //can be colered with red,green
    private String shapeType;
    public Circle(String shapeType, Color color) {
        super(color);
        this.shapeType = shapeType;
    }

    public void fillColor() {
        color.colorShape(shapeType);
    }
}

 

public class Square extends Shape { //can be colered with red,green
    private String shapeType;
    public Square(String shapeType, Color color) {
        super(color);
        this.shapeType = shapeType;
    }

    public void fillColor() {
        color.colorShape(shapeType);
    }
}

Step 5:- Finally create main() for configuaration

public class BridgePatternDemo {
    public static void main(String[] args) {
         // circle is fine level abstarction that uses Redcolor concrete implementation of Implementer
        Shape redCircle = new Circle("Circle", new RedColor()); 
        Shape greenCircle = new Circle("Circle", new GreenColor());
        Shape redSquare = new Square("Square", new RedColor());
        Shape greenSquare = new Square("Square", new GreenColor());
        redCircle.colorShape();
        greenCircle.colorShape();
        redSquare.colorShape();
        greenSquare.colorShape();
    }
}

About Author

Author Image
Mohit Jain

Mohit Jain working as backend java developer having knowlege of core java,spring,hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..