两个维度的桥接模式

 

用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。

实验要求:

1.  画出对应的类图;

 

2.  提交源代码;

public class Bus implements Vehicle

{

@Override

public void drive()

{

System.out.print("大巴");

}

}

public class Car implements Vehicle

{

@Override

public void drive()

{

System.out.print("小轿车");

}

}

public class CementRoad extends Road

{

public CementRoad(Vehicle vehicle)

{

super(vehicle);

}

@Override

public void driveOnRoad()

{

super.vehicle.drive();

System.out.println("行驶在水泥路");

}

}

public class main {

public static void main(String[] args) {

Road road = new CementRoad(new Car());

road.driveOnRoad();

}

}

public abstract class Road

{

protected Vehicle vehicle;

 

public Road(Vehicle vehicle)

{

this.vehicle = vehicle;

}

public abstract void driveOnRoad();

}

public class UnpavedRoad extends Road

{

public UnpavedRoad(Vehicle vehicle)

{

super(vehicle);

}

@Override

public void driveOnRoad()

{

super.vehicle.drive();

System.out.println("行驶在石子路");

}

 

public interface Vehicle {

public void drive();

 

}

posted @ 2024-11-05 14:25  涨涨涨张  阅读(3)  评论(0编辑  收藏  举报