空对象设计模式

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; //抛出空对象,可以让调用者免于判空
        }
        //....其他判断代码
        
    }
}

 

posted @ 2021-01-25 10:41  日进一卒  阅读(72)  评论(0)    收藏  举报