Java – null object design pattern vs null object check
Why is null object design pattern better than null object checking
Null Object design pattern
Solution
The whole problem with null is that if you try to access a null value, the application throws a NullPointerException and aborts
To reduce the number of classes nullxxx in this null object design pattern (actually just factory design data, not the pattern itself), you can create a static final nullcustomer that always returns
In Java 8, you can use the optional method to determine when a function does not always return a value This approach does not force you to create any empty classes that pollute the overall structure (considering that you may also have to refactor those empty classes)
Eclipse and IntelliJ also provide compile time annotations @ nullable and @ nonnull, which give compiler warnings when accessing potentially empty objects However, many frameworks have no annotations Therefore, IntelliJ attempts to use static analysis to discover those potential null accesses In addition to this approach, IntelliJ and eclipse use their own annotations (org. Eclipse. JDT. Annotation. Nonnull, com. IntelliJ. Annotations. Notnull), which are incompatible However, you can use store the annotations outside of the code in IntelliJ Eclipse also hopes to achieve this in the future The problem is that many frameworks provide this feature to give you many different comments to do the same thing Jsr-305 sleep It provides a comment in javax I don't know why they didn't push this further