Member-only story
Design Patterns: Null Object
Avoid conditional complexity using this pattern
There are 23 classic design patterns, which are described in the original book, âDesign Patterns: Elements of Reusable Object-Oriented Software.â These patterns provide solutions to particular problems, often repeated in the software development.
In this article, I am going to describe what the Null-Object Pattern is and how and when it should be applied. This pattern is not included in the classic pattern book, but it was first published in the âPattern Languages of Program,â and it is widely used to avoid complexity.
Basic Idea
In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (ânullâ) behaviour. The null object design pattern describes the uses of such objects and their behaviour (or lack thereof).âWikipedia
The main feature of this pattern is that this allows us to avoid complexity in our code. In most languages, such as Java, C# or JavaScript, the references may be null. Depending on our business logic, checking the code may be necessary to ensure they are not null before invoking any methods because methods typically cannot be invoked on null references.