ResponsibleChain(责任链模式)
/** * 责任链模式 * @author TMAC-J * 老板讲任务交给CTO,CTO自然不会亲自去做,又把人物分配给项目经理,项目经理再把任务分配给组长,组长再分配给个人 * 如果中途哪个环节出现了问题,比如项目经理请假了,组长直接接上,使得项目更加灵活 */ public class ResponsibleChain { abstract class Handler{ abstract void setNextHandler(Handler nextHandler); abstract Handler getNextHandler(); abstract void doHandler(String str); } public class CTOHanlder extends Handler{ private Handler nextHandler; @Override void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } @Override Handler getNextHandler() { return nextHandler; } @Override void doHandler(String str) { //其实这部分可以用模块模式分出来,但是这样会增加复杂度,所以此处不用 if("人手足够".equals(str)){ nextHandler.doHandler(str); } else{ System.out.println("我自己处理.."); } } } public class ManageHanlder extends Handler{ private Handler nextHandler; @Override void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } @Override Handler getNextHandler() { return nextHandler; } @Override void doHandler(String str) { //其实这部分可以用模块模式分出来,但是这样会增加复杂度,所以此处不用 if("人手足够".equals(str)){ nextHandler.doHandler(str); } else{ System.out.println("我自己处理.."); } } } public class WorkerHandler extends Handler{ private Handler nextHandler; @Override void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } @Override Handler getNextHandler() { return nextHandler; } @Override void doHandler(String str) { System.out.println("手下没人了,我自己干吧...总有一天我也要当CTO!!!"); } } public class Boss{ public void dispatchMission(){ Handler ctoHandler = new CTOHanlder(); Handler managerHandler = new ManageHanlder(); Handler workerHandler = new WorkerHandler(); ctoHandler.setNextHandler(managerHandler); ctoHandler.doHandler("人手足够"); managerHandler.setNextHandler(workerHandler); managerHandler.doHandler("人手足够"); workerHandler.doHandler("反正没人了随便写!!!"); } } }