Bridge Pattern
Definition -
The basis for a bridge is to decouple an abstract class from the place where it is implemented. This means the abstraction should not be binded with the implementation at compile time, rather it should be done at runtime depending on the place it is invoked.
Scenario -
Now lets take a scenario of a factory line which does both production and designing of both hatcback and sedan.
1) In this case we can consider it as two abstract entities, one is vehicle and the other is type.
vehicle.java
type.java
2) hatcback and sedan are two concrete classes which will implement the vehicle interface.
hatchback.java
sedan.java
3) production and designing are two concrete classes which will implement the type interface.
production.java
design.java
4) Lets see how to implement this in a client
bridgedemo.java
Place to use -
1) When you need abstraction
2) When you do not want to bind the classes at compile time
3) When you want hierarchies to interact with each other
Place not to use -
1) Increases complexity
Did not find any other aspect to this, you are welcome with you suggestions.
The code for this can be found in github in the link here
Definition -
The basis for a bridge is to decouple an abstract class from the place where it is implemented. This means the abstraction should not be binded with the implementation at compile time, rather it should be done at runtime depending on the place it is invoked.
Scenario -
Now lets take a scenario of a factory line which does both production and designing of both hatcback and sedan.
1) In this case we can consider it as two abstract entities, one is vehicle and the other is type.
vehicle.java
package BridgePattern;
public abstract class vehicle {
public type type1;
public type type2;
public vehicle(type type1, type type2) {
this.type1=type1;
this.type2=type2;
}
public abstract void manufacture();
}
type.java
package BridgePattern;
public interface type {
public void manufacture();
}
2) hatcback and sedan are two concrete classes which will implement the vehicle interface.
hatchback.java
package BridgePattern;
public class hatchback extends vehicle {
public hatchback(type type1, type type2) {
super(type1, type2);
}
@Override
public void manufacture() {
System.out.println("Started Manufacturing a hatchback");
type1.manufacture();
type2.manufacture();
}
}
sedan.java
package BridgePattern;
public class sedan extends vehicle {
public sedan(type type1, type type2) {
super(type1, type2);
}
@Override
public void manufacture() {
System.out.println("Started Manufacturing a sedan");
type1.manufacture();
type2.manufacture();
}
}
3) production and designing are two concrete classes which will implement the type interface.
production.java
package BridgePattern;
public class production implements type {
@Override
public void manufacture() {
System.out.println("Started production");
}
}
design.java
package BridgePattern;
public class design implements type {
@Override
public void manufacture() {
System.out.println("Started design");
}
}
4) Lets see how to implement this in a client
bridgedemo.java
package BridgePattern;
public class bridgedemo {
public static void main(String[] args) {
vehicle v1=new hatchback(new design(),new production());
v1.manufacture();
}
}
Place to use -
1) When you need abstraction
2) When you do not want to bind the classes at compile time
3) When you want hierarchies to interact with each other
Place not to use -
1) Increases complexity
Did not find any other aspect to this, you are welcome with you suggestions.
The code for this can be found in github in the link here
No comments:
Post a Comment