abstract class Vehicle
{
VehicleImpl aVehicleImpl=null;
public Vehicle(VehicleImpl vehicleImpl)
{
aVehicleImpl=vehicleImpl;
}
void transport()
{
aVehicleImpl.transportImpl();
}
}
interface VehicleImpl
{
void transportImpl();
}
class TransportPeople implements VehicleImpl
{
public void transportImpl()
{
System.out.println("people");
}
}
class TransportGoods implements VehicleImpl
{
public void transportImpl()
{
System.out.println("goods");
}
}
class Car extends Vehicle
{
VehicleImpl aVehicleImpl=null;
public Car(VehicleImpl vehicleImpl)
{
super(vehicleImpl);
}
public void transport()
{
System.out.print("car transport ");
super.transport();
}
}
class Train extends Vehicle
{
VehicleImpl aVehicleImpl=null;
public Train(VehicleImpl vehicleImpl)
{
super(vehicleImpl);
}
public void transport()
{
System.out.print("Train transport ");
super.transport();
}
}
public class BridgePatternTest
{
public static void main(String[] args)
{
Vehicle aVehicle=new Car(new TransportPeople());
aVehicle.transport();
aVehicle=new Car(new TransportGoods());
aVehicle.transport();
aVehicle=new Train(new TransportPeople());
aVehicle.transport();
aVehicle=new Train(new TransportGoods());
aVehicle.transport();
}
}
//////////////////////////////////////////////////
输出结果:
car transport people
car transport goods
Train transport people
Train transport goods