To avoid NullPointerExceptions I find it useful to provide an immutable 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With