现在我们很多家庭都有了榨汁机。榨汁的过程大体上为水果切片-->放入榨汁机-->加入调味料-->启动。可根据需要加入蜂蜜或苏打水,也可以什么调料都不加,制作纯果汁。请用模板方法模拟这个过程
AbstractClass.java
package Fruit;public abstract class AbstractClass {public void primitiveOperation1() {System.err.println("切片");}public void primitiveOperation2() {System.err.println("放入榨汁机");}public abstract void primitiveOperation3();public void primitiveOperation4() {System.err.println("启动");}public void templateMethod() {primitiveOperation1();primitiveOperation2();primitiveOperation3();primitiveOperation4();}
}
ConcreteClassA.java
package Fruit;public class ConcreteClassA extends AbstractClass {@Overridepublic void primitiveOperation3() {// TODO Auto-generated method stubSystem.err.println("加入蜂蜜");}}
ConcreteClassB.java
package Fruit;public class ConcreteClassB extends AbstractClass {@Overridepublic void primitiveOperation3() {// TODO Auto-generated method stubSystem.err.println("加入苏打水");}}
ConcreteClassC.java
package Fruit;public class ConcreteClassC extends AbstractClass {@Overridepublic void primitiveOperation3() {// TODO Auto-generated method stubSystem.err.println("什么都不加");}}
Client.java
package Fruit;public class Client {public static void main(String[] args) {// TODO Auto-generated method stubAbstractClass a1,a2,a3;a1=new ConcreteClassA();a1.templateMethod();System.err.println("-----------------------");a2=new ConcreteClassB();a2.templateMethod();System.err.println("-----------------------");a3=new ConcreteClassC();a3.templateMethod(); }}
运行截图: