Skip to content

Latest commit

 

History

History
74 lines (52 loc) · 1.54 KB

File metadata and controls

74 lines (52 loc) · 1.54 KB

Purpose

NullObject is not a GoF design pattern but a schema which appears frequently enough to be considered a pattern. It has the following benefits:

  • Client code is simplified
  • Reduces the chance of null pointer exceptions
  • Fewer conditionals require less test cases

Methods that return an object or null should instead return an object or NullObject. NullObjects simplify boilerplate code such as if (!is_null($obj)) { $obj->callSomething(); } to just $obj->callSomething(); by eliminating the conditional check in client code.

Examples

  • Null logger or null output to preserve a standard way of interaction between objects, even if the shouldn't do anything
  • null handler in a Chain of Responsibilities pattern
  • null command in a Command pattern

UML Diagram

Alt NullObject UML Diagram

Code

You can also find this code on GitHub

Service.php

Service.php

Logger.php

Logger.php

PrintLogger.php

PrintLogger.php

NullLogger.php

NullLogger.php

Test

Tests/LoggerTest.php

Tests/LoggerTest.php