Null Object Pattern
Provide default behavior for null references
Overview
The Null Object Pattern replaces null value checking with evaluating a "Null" representation of an object. This reduces the number of null checks and makes code cleaner.
Key Concepts
Provides a safe default behavior instead of returning null
Implements the same interface as the real object
Code Example
java
1public interface Animal { void makeSound(); }
2public class Dog implements Animal { public void makeSound() { System.out.println("Woof"); } }
3public class NullAnimal implements Animal { public void makeSound() { /* Do nothing */ } }