According to GoF definition: bridge pattern is ,decouple an abstraction from its implementation
so that the two can vary independently.
我实在是理解无力, 还是看code吧。
Usage in Jdk:
AWT (It provides an abstraction layer which maps onto the native OS the windowing support)
JDBC
-
package BridgePattern;
-
-
interface VehicleType{
-
abstract public void book();
-
}
-
-
abstract class Vehicle{
-
protected VehicleType type1;
-
protected VehicleType type2;
-
-
public Vehicle(VehicleType type1, VehicleType type2){
-
this.type1 = type1;
-
this.type2 = type2;
-
}
-
-
abstract public void purchase();
-
}
-
-
-
class Car extends Vehicle{
-
//Constructor
-
public Car(VehicleType type1, VehicleType type2){
-
super(type1, type2);
-
}
-
-
@Override
-
public void purchase(){
-
System.out.println("car");
-
type1.book();
-
type2.book();
-
}
-
}
-
-
class Bike extends Vehicle{
-
public Bike(VehicleType type1, VehicleType type2){
-
super(type1, type2);
-
}
-
-
@Override
-
public void purchase(){
-
System.out.println("Bike");
-
type1.book();
-
type2.book();
-
}
-
}
-
-
-
class NewVehicle implements VehicleType{
-
-
@Override
-
public void book(){
-
System.out.println("New Vehicle");
-
}
-
}
-
-
class OldVehicle implements VehicleType{
-
@Override
-
public void book(){
-
System.out.println("Old Vehicle");
-
}
-
}
-
-
public class BridgePatternDemo {
-
public static void main(String[] args){
-
Vehicle vehicle1 = new Car(new NewVehicle(), new OldVehicle());
-
vehicle1.purchase();
-
-
Bike bike1 = new Bike(new NewVehicle(), new OldVehicle());
-
bike1.purchase();
-
}
-
}
阅读(495) | 评论(0) | 转发(0) |