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);
}

Bachelor in Computer Science, MBA in Software Architecture and .NET Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introduction: What is the Cluster Etcd Operator ?

What are Pinned objects and Pinned Object Heaps in C#?

All about static libraries in C

LEARN TO DETACH AND GROW | STRIVE FOR SIMPLICITY NOT PERFECTION

Use Min-Width, Not Max-Width, in Your CSS

Are Outsourced Java Developers Still in Demand — Let’s Check Out

Outsourced Java Developers Still in Demand

Is Your Scrum Master Making These Mistakes?

FREE: Creative Coding Classes for Beginners

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
Alex Alves

Alex Alves

Bachelor in Computer Science, MBA in Software Architecture and .NET Developer.

More from Medium

How To: Breaking ASP.NET Core with Angular Project into Frontend and Backend

CQRS Pattern Implementation

[Dot Net Core](Graphic series)9. Comparison of Dot Net Core MVC and Asp.NET MVC-Timing of Factory

View Components in ASP.NET Core