Null Object Design Pattern

Null Object is an object with defined neutral (“null”) behavior(via url).You also known as Stub,  Active Null,  Active Nothing. An alternative to object null controls(validation,check)

Let’s look codes

Nested type NullStaff class in StaffBase abstract class doesn’t consist of operational code and set default value for variable types(for instance int=0,String.Empty)

In the below you can see class diagram


Null Object stands in for Real object.In our example Real Object is StaffBase abstract class and you can define as interface.Actually in that way our codes is more pure and cleaner.If you check often validation or null control for specific objects,You can use null object pattern.

Null Object pattern related  strategy pattern,command pattern and state pattern(for special cases).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.