The Bridge Pattern allows you to vary the implementation and abstraction by placing the two in seperate hierarchies.
Decouple an abstraction or interface from its implementation so that the two can vary independently. It is a structural design pattern.
The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.
Example
The bridge pattern can be demonstrated with an example. Suppose we have a Vehicle class. We can extract out the implementation of the engine into an Engine class. We can reference this Engine implementor in our Vehicle via an Engine field. We'll declare Vehicle to be an abstract class. Subclasses of Vehicle need to implement the drive() method. Notice that the Engine reference can be changed via the setEngine() method.
Example Code for Bridge Pattern
Consider a class called Vehicle:
public abstract class Vehicle {
//Vehicle have components like engine, tyre
//add other components like headlight, music system etc..
//but I just took 2 for simplicity
Engine engine;
Tyre tyre;
//assembling cose is fixed here, but can be variable
float assemblingCost;
public abstract void drive();
public void setEngine(Engine engine) {
this.engine = engine;
}
public void setTyres(Tyre tyre)
{
this.tyre = tyre;
}
public void assembleVehicle(Engine engine,Tyre tyre)
{
this.engine=engine;
this.tyre = tyre;
}
public float getPrice()
{
return tyre.getCost()+engine.getCost()+assemblingCost;
}
}
public class BigTruck extends Vehicle {
public BigTruck(Engine engine,Tyre tyre) {
this.assemblingCost = 3000;
assembleEngine(engine,tyre);//calls assembleEngine
// from base class
}
@Override
public void drive() {
System.out.println("Driving the truck now, with cost:"+
getPrice();
}
}
public class SmallCar extends Vehicle {
public SmallCar(Engine engine,Tyre tyre) {
this.assemblingCost = 2000;
assembleEngine(engine,tyre);//calls assembleEngine
// from base class
}
@Override
public void drive() {
System.out.println("Driving the car now, with cost:"+
getPrice();engine.kickStart();
tyre.roll();
}
}
Now getting the components of the cars:
Engine of the car:
public interface Engine {
public int kickStart();
}
public class BigEngine implements Engine {
int horsepower;
public BigEngine() {
horsepower = 350;
}
@Override
public int kickStart() {
System.out.println("The big engine is running");
return horsepower;
}
}
public class SmallEngine implements Engine {
int horsepower;
public SmallEngine() {
horsepower = 100;
}
@Override
public int kickStart() {
System.out.println("The small engine is running");
return horsepower;
}
}
public interface Tyre{
public void roll();
}
public class ThickTyre implements Tyre{
private int width;
public ThickTyre(){
width=200;
}
public void roll(){
System.out.println("Rolling on thick tyres of width:"
+ width);
}
}
public class MediumTyre implements Tyre{
private int width;
public MediumTyre (){
width=140;
}
public void roll(){
System.out.println("Rolling on thin tyres of width:"
+ width);
}
}
public class BridgeDemo {
public static void main(String[] args) {
Vehicle vehicle = new BigTruck(new SmallEngine(),new ThickTyre());
vehicle.drive();
vehicle.assembleVehicle(new BigEngine(),new ThickTyre());
vehicle.drive();
vehicle = new SmallCar(new SmallEngine(),new MediumTyre());
vehicle.drive();
vehicle.assembleVehicle(new BigEngine(),new MediumTyre());
vehicle.drive();
}
}
No comments:
Post a Comment