Dummy implementation design pattern name
To avoid NullPointerExceptions I find it useful to provide an immutab开发者_JAVA技巧le dummy implementation of an interface together with the interface. Like this:
public interface Action {
void perform();
public static final Action dummy = new Action() {
public void perform() {
/*nothing*/
}
};
}
Action.dummy
can then be used instead of the evil null
.
Is there a name for this design pattern?
Null Object pattern (provided by M. Fowler if I remember correctly).
Here is a chapter Introduce Null Object from Fowler's Refactoring book.
精彩评论