publicclassRadioAssemblyLineextendsAssemblyLine{ /* 生产收音机元器件和天线 */ @Override protectedvoidonProduceComponents(){ System.out.println("Product Radio Components and Antennas"); } }
在这里,只重写了一个生产元器件的方法
再来看一下生产电脑的流水线,为了区分与生产收音机的流水线,这里多重写几个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassComputerAssemblyLineextendsAssemblyLine{ /* 生产铝合金外壳和液晶显示屏 */ @Override protectedvoidonProduceShell(){ System.out.println("Product Aluminum housing and Liquid Crystal Display"); } /* 生产元器件和键盘 */ @Override protectedvoidonProduceComponents(){ System.out.println("Product Components and keyboard"); } /* 将产品打包并标上苹果标签 */ @Override protectedvoidonProductPacking(){ System.out.println("Pack and Mark the Apple trademark"); } }
这里多重写了产品装配方法和产品打包方法
可以看到的是,在这两个具体的流水线中,都只写了区别于其他流水线的代码,代码更加的简洁了
因为,流水线的工作流程都已经封装好了,那么在接下来的测试中,只需要直接调用就可以了
1 2 3 4 5 6 7
AssemblyLine assemblyLine = new RadioAssemblyLine(); assemblyLine.product();
System.out.println();
assemblyLine = new ComputerAssemblyLine(); assemblyLine.product();
看一下输出结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
+------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------+