Null object is an object that has no state and does nothing. It acts as a surrogate for the lack of an object of a given type.
It can be used from the client to prevent the nasty null object reference at runtime in case clients forget to check for null, therefore, it's a very smart null object.
using System;
namespace Test
{
public class Customer
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public Customer(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
}
public abstract class BaseObject
{
public abstract object GetCustomer();
}
public class NonNullObject : BaseObject
{
private object Customer;
public NonNullObject(object Customer)
{
this.Customer = Customer;
}
public override object GetCustomer()
{
return (object)Customer;
}
}
public class NullObject : BaseObject
{
private static NullObject Instance = new NullObject();
public static NullObject GetInstance()
{
return Instance;
}
public override object GetCustomer()
{
Console.WriteLine("Null object has no state and does nothing");
return this;
}
}
public class Context
{
private BaseObject Strategy;
public Context(BaseObject Strategy)
{
this.Strategy = Strategy;
}
public Object GetCustomer()
{
return Strategy.GetCustomer();
}
}
public class Test
{
public static void Main(string[] Args)
{
Customer Cust = new Customer(1, "Thanh");
Context obj = new Context(new NonNullObject(Cust));
Customer Temp = (Customer)obj.GetCustomer();
Console.WriteLine("Id: {0} Name: {1}", Temp.Id.ToString(), Temp.Name);
obj = new Context(NullObject.GetInstance());
obj.GetCustomer();
}
}
}
1. Create the abstract base class or interface
2. Create a child class that inherits from the base class. Instantiating itself as a singleton and provide a method for clients to get an instance of it.
3. Clients can now treat the null object as any other valid object avoiding the nasty null object reference at runtime.
Hope it helps