空对象设计模式
interface Action { public void doSomething(); } public class Main { private Action doNothingObject = new Action() { @Override public void doSomething() { //什么也不做 } }; public Action actionJudge(String actionKeyword) { if("doNothing".equals(actionKeyword)){ return this.doNothingObject; //抛出空对象,可以让调用者免于判空 } //....其他判断代码 } }

浙公网安备 33010602011771号