Design Patterns: Null Object

Normal Situation

public void CreateClient(IClient client)
{
if (client == null) throw new NullReferenceException();
if (client.Name == null) throw new NullReferenceException();
// ... do something
}

Null Object Pattern

public class NullClient : IClient
{
public int Id => 0;
public string Name => "Non-client"; public int Age => 0;
}
static void Main(string[] args)
{
IClient client = new Client();
var clientService = new ClientService(); if (client == null)
client = new NullClient();
clientService.CreateClient(client);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store