public interface PhoneMakerAssembly {
void setNextPoint(PhoneMakerAssembly successor);
PhoneMakerAssembly getNextPoint();
void build(String partName);
}
public abstract class AbstractPhoneMakerAssembly implements PhoneMakerAssembly {
private PhoneMakerAssembly nextpoint;
@Override public void setNextPoint(PhoneMakerAssembly successor) {
nextpoint=successor;
}
@Override public PhoneMakerAssembly getNextPoint() {
return nextpoint;
}
}
public class Framemaker extends AbstractPhoneMakerAssembly{
@Override public void build(String partName) {
if ("main".equals(partName)){
System.out.println("构建主体框架");
return;
}
System.out.println("构建失败,寻找下家");
getNextPoint().build(partName);
}
}
public class CameraMaker extends AbstractPhoneMakerAssembly{
@Override public void build(String partName) {
if ("camera".equals(partName)){
System.out.println("构建摄像头框架");
return;
}
System.out.println("构建失败,寻找下家");
getNextPoint().build(partName);
}
}
public class ScreenMaker extends AbstractPhoneMakerAssembly{
@Override public void build(String partName) {
if ("screen".equals(partName)){
System.out.println("构建屏幕框架");
return;
}
System.out.println("构建失败,寻找下家");
getNextPoint().build(partName);
}
}
public class DefaultMaker implements PhoneMakerAssembly {
@Override public void setNextPoint(PhoneMakerAssembly successor) {
}
@Override public PhoneMakerAssembly getNextPoint() {
return null;
}
@Override public void build(String partName) {
System.out.println("未找到支持的构建器");
}
}
public class App {
public static void main(String[] args) {
PhoneMakerAssembly maker1=new Framemaker();
PhoneMakerAssembly maker2=new CameraMaker();
PhoneMakerAssembly maker3=new ScreenMaker();
maker1.setNextPoint(maker2);
maker2.setNextPoint(maker3);
maker3.setNextPoint(new DefaultMaker());
maker1.build("screen");
maker1.build("main");
maker1.build("camera");
maker1.build("xxx");
}
}