实例说明:某公司欲开发一款儿童玩具汽车,为了更好地吸引小朋友的注意力,该玩具汽车在移动过程中伴随着灯光闪烁和声音提示。在该公司以往的产品中已经实现了控制灯光闪烁(例如警灯闪烁)和声音提示(例如警笛音效)的程序,为了重用先前的代码并且使得汽车控制软件具有更好的灵活性和扩展性。
1、类图:
2、源代码:
public class Client {
public static void main(String[] args) {
CarController carController = new PoliceCarAdapter(new PoliceLamp(),new PoliceSound());
carController.move();
carController.phonate();
carController.twinkle();
}
}
abstract class CarController {
public void move(){
System.out.println("汽车跑起来了");
}
public abstract void phonate();
public abstract void twinkle();
}
class PoliceSound{
pu