汽车品牌内置导航仪,我们希望实现,每个品牌的导航仪都可以在任何一个牌子的汽车上安装并启动。汽车品牌有两个:宝马、奔驰。导航仪有三个牌子:神行者、北斗、高德
AbstractVehicleBrand.java
package car;public abstract class AbstractVehicleBrand { public abstract void install(INavigator n); public abstract void open();
}
INavigator.java
package car;public interface INavigator { public void work();
}
GDNavigator.java
package car;public class GDNavigator implements INavigator {@Overridepublic void work() {// TODO Auto-generated method stubSystem.out.println("---开启高德导航!"); }}
BDNavigato.java
package car;public class BDNavigator implements INavigator {@Overridepublic void work() { System.out.println("---开启北斗导航!"); } }
SXZNavigator.java
package car;public class SXZNavigator implements INavigator {@Overridepublic void work() {// TODO Auto-generated method stubSystem.out.println("---开启神行者导航!"); }}
BCWVehicle.java
package car;public class BCWVehicle extends AbstractVehicleBrand {private INavigator navigator = null; String name="奔驰";@Overridepublic void install(INavigator n) {// TODO Auto-generated method stubnavigator = n;}@Overridepublic void open() {// TODO Auto-generated method stubSystem.err.println(name);navigator.work();}}
BMWVehicle.java
package car;public class BMWVehicle extends AbstractVehicleBrand {private INavigator navigator = null; String name="宝马";@Overridepublic void install(INavigator n) {// TODO Auto-generated method stubnavigator = n;}@Overridepublic void open() {// TODO Auto-generated method stubSystem.err.println(name);navigator.work();}
Client.java
package car;public class Client {public static void main(String[] args) {// TODO Auto-generated method stubINavigator a1=new GDNavigator();AbstractVehicleBrand bmw=new BMWVehicle();bmw.install(a1);bmw.open();INavigator a2=new BDNavigator();AbstractVehicleBrand bc=new BCWVehicle();bc.install(a2);bc.open();}}
运行截图: