public class RadioAssemblyLine extends AssemblyLine {
/* 生产收音机元器件和天线 */
@Override
protected void onProduceComponents() {
System.out.println("Product Radio Components and Antennas");
}
}
在这里,只重写了一个生产元器件的方法
再来看一下生产电脑的流水线,为了区分与生产收音机的流水线,这里多重写几个方法
public class ComputerAssemblyLine extends AssemblyLine {
/* 生产铝合金外壳和液晶显示屏 */
@Override
protected void onProduceShell() {
System.out.println("Product Aluminum housing and Liquid Crystal Display");
}
/* 生产元器件和键盘 */
@Override
protected void onProduceComponents() {
System.out.println("Product Components and keyboard");
}
/* 将产品打包并标上苹果标签 */
@Override
protected void onProductPacking() {
System.out.println("Pack and Mark the Apple trademark");
}
}
这里多重写了产品装配方法和产品打包方法
可以看到的是,在这两个具体的流水线中,都只写了区别于其他流水线的代码,代码更加的简洁了
因为,流水线的工作流程都已经封装好了,那么在接下来的测试中,只需要直接调用就可以了
AssemblyLine assemblyLine = new RadioAssemblyLine();
assemblyLine.product();
System.out.println();
assemblyLine = new ComputerAssemblyLine();
assemblyLine.product();
看一下输出结果
+------Start Product------+
Produce Shell
Product Radio Components and Antennas
Assembly Components
Test Products
Product Radio Components and Antennas
Product Packing
+------Finish Product------+
+------Start Product------+
Product Aluminum housing and Liquid Crystal Display
Product Components and keyboard
Assembly Components
Test Products
Product Components and keyboard
Pack and Mark the Apple trademark
+------Finish Product------+