1、CarModel.java
package com.ssm.cts.pattern.template; public abstract class CarModel { // 启动 public abstract void start(); // 停止 public abstract void stop(); // 喇叭声音 public abstract void alarm(); // 引擎声音 public abstract void engineBoom(); // 运行 public void run(){ this.start(); this.engineBoom(); this.alarm(); this.stop(); } }
2、BenzModel.java
package com.ssm.cts.pattern.template; public class BenzModel extends CarModel { @Override public void alarm() { System.out.println("奔驰:喇叭声音..."); } @Override public void engineBoom() { System.out.println("奔驰:引擎音声音..."); } @Override public void start() { System.out.println("奔驰:启动..."); } @Override public void stop() { System.out.println("奔驰:停止"); } }
3、BMWModel.java
package com.ssm.cts.pattern.template; public class BMWModel extends CarModel { @Override public void alarm() { System.out.println("宝马:喇叭声音..."); } @Override public void engineBoom() { System.out.println("宝马:引擎音声音..."); } @Override public void start() { System.out.println("宝马:启动..."); } @Override public void stop() { System.out.println("宝马:停止"); } }
4、Demo.java
package com.ssm.cts.pattern.template; public class Demo { // 测试 public static void main(String[] args) { CarModel car = new BenzModel(); car.run(); } }