java中23种设计模式之10-桥梁模式(bridge pattern)
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。