0

In Bridge pattern i have read "decouples abstraction from implementation so that both can vary independently". what does it mean really ? and how does this phrase related to the Bridge pattern example given below? i understand this code but unable to relate this code with phrase. can anyone explain this.

Thanks.

/* Implementor interface*/    


interface Gear    
{
 void handleGear();
}

/* Concrete Implementor - 1 */

class ManualGear implements Gear{

public void handleGear(){

    System.out.println("Manual gear");
}
}

/* Concrete Implementor - 2 */

class AutoGear implements Gear{

public void handleGear(){
    System.out.println("Auto gear");
}
}

/* Abstraction (abstract class) */

abstract class Vehicle {
Gear gear;

public Vehicle(Gear gear){

    this.gear = gear;
}

abstract void addGear();
}

   /* RefinedAbstraction - 1*/

    class Car extends Vehicle{

public Car(Gear gear){

    super(gear);

    // initialize various other Car components to make the car
}

public void addGear(){

    System.out.print("Car handles ");

    gear.handleGear();

    }
}

/* RefinedAbstraction - 2 */

class Truck extends Vehicle{

public Truck(Gear gear){

    super(gear);

    // initialize various other Truck components to make the car

}

public void addGear(){
    System.out.print("Truck handles " );
    gear.handleGear();
}
}

/* Client program */

public class BridgeDemo {  

public static void main(String args[]){

    Gear gear = new ManualGear();

    Vehicle vehicle = new Car(gear);

    vehicle.addGear();

    gear = new AutoGear();
    vehicle = new Car(gear);
    vehicle.addGear();

    gear = new ManualGear();
    vehicle = new Truck(gear);
    vehicle.addGear();

    gear = new AutoGear();
    vehicle = new Truck(gear);
    vehicle.addGear();
}
}

output
Car handles Manual gear
Car handles Auto gear
Truck handles Manual gear
Truck handles Auto gear
Graham
  • 7,431
  • 18
  • 59
  • 84

1 Answers1

0

"decouples abstraction from implementation so that both can vary independently"

In this specific code example, this means that Vehicle class only depends on the Gear interface, not on the specific implementations of it. This decouples the contract (how various vehicles use various gears, i.e. by calling handleGear()) from the private implementation of different types of gears.

In more practical terms, it means that changes to Gear implementations do not require to recompile the code where Vehicle is defined, as long as the interface stays the same. You can perfectly add a new type of gear and pass it to the same existing vehicle - the contract is unchanged and the actual implementation can be determined at runtime.

Same holds for Vehicle: you can keep changing the private implementations of them, as long as the way Gear is used doesn't change. Hence, both Gear and Vehicle implementations can vary independently.

Alexander Tsvetkov
  • 1,649
  • 14
  • 24
  • Hi Alexander Tsvetkov, thanks for the reply. i think this is the best answer. and now i am able to understand this concept completely. again thanks. – mukesh singh Jun 15 '17 at 12:39
  • @mukeshsingh, I glad that it helped! Please consider upvoting and marking the answer as accepted. – Alexander Tsvetkov Jun 16 '17 at 10:35
  • Why not Alexander ! i will surly do that but how to do ?. i am new in stackoverflow. don't know about upvoting and how to mark as accepted. – mukesh singh Jun 16 '17 at 11:37