© Dmitri Nesteruk 2019
Dmitri NesterukDesign Patterns in .NEThttps://doi.org/10.1007/978-1-4842-4366-4_20

20. Null Object

Dmitri Nesteruk1 
(1)
St. Petersburg, c.St-Peterburg, Russia
 

We don’t always choose the interfaces we work with. For example, I would rather have my car drive me to my destination by itself, without me having to give 100 percent of my attention to the road and the dangerous lunatics driving next to me. It’s the same with software: sometimes you don’t really want a piece of functionality, but it is built into the interface. So what do you do? You make a Null Object.

Scenario

Suppose you inherit a library that uses the following interface:
public interface ILog
{
  void Info(string msg);
  void Warn(string msg);
}
The library uses this interface ...

Get Design Patterns in .NET: Reusable Approaches in C# and F# for Object-Oriented Software Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.