Replacing Repeated Absence Checks with the Null Object Pattern
Optional collaborators often begin innocently. A service may have a notifier when notifications are enabled, a metrics recorder when monitoring is configured, or an audit sink in environments that need auditing. The first absence check is easy to understand. The twentieth can make the real behavior harder to see. The Null Object pattern is one way to remove that repetition. Instead of representing “no collaborator” with a null-like value and asking every caller to handle it, provide an object that implements the same contract with behavior appropriate for absence.