Null Object Design Pattern Example in C#

Null checks are quite common in the code I write. It litters up my class methods with all kinds of exception handling. The Null Object Design Pattern helps me to avoid this checking for null. It provides a non functional object to the client instead of null. So I can call a method on this object that effectively does nothing. This is what I learned.

Create a base class (or interface) with an embedded null object, returning default value on null from a static read only property. Create a few objects derived from that base class and implement the abstract properties and methods. From your Repository return the Null Object instead of null whenever needed. In the example the Null Object gets returned on a find to the ‘Restless’ Person.

using System;

namespace NullObject
{
    class Program
    {
        static void Main(string[] args)
        {
            PersonRepository personRepository = new PersonRepository();
            var oldPerson = personRepository.Find("Old");
            var youngPerson = personRepository.Find("Young");
            var restlessPerson = personRepository.Find("Restless");

            oldPerson.Talk();
            youngPerson.Talk();
            restlessPerson.Talk();

            oldPerson.ShutUp();
            youngPerson.ShutUp();
            restlessPerson.ShutUp();

            Console.Read();
        }
    }

    public abstract class PersonBase
    {
        public abstract string Name { get; }
        public abstract int Age { get; }
        public abstract void Talk();
        public abstract void ShutUp();

        #region Null implementation

        // Singleton
        static readonly NullPerson nullPerson = new NullPerson();

        public static NullPerson Null
        {
            get { return nullPerson; }
        }

        // Embedded Null Object class
        public class NullPerson : PersonBase
        {
            public override string Name { get { return string.Empty; } }
            public override int Age { get { return 0; } }
            public override void Talk() { }
            public override void ShutUp() { }
        }

        #endregion
    }

    public class OldPerson : PersonBase
    {

        public override string Name
        {
            get { return "Grandpa"; }
        }

        public override int Age
        {
            get { return 80; }
        }

        public override void Talk()
        {
            Console.WriteLine(
                "Bladibla uche uche bla... I'm {0} years old", this.Age);
        }

        public override void ShutUp()
        {
            Console.WriteLine("{0} stopped talking", this.Name);
        }
    }

    public class YoungPerson : PersonBase
    {

        public override string Name
        {
            get { return "Little girl"; }
        }

        public override int Age
        {
            get { return 7; }
        }

        public override void Talk()
        {
            Console.WriteLine(
                "Bladibla hihihihihi bla... I'm {0} years old", this.Age);
        }

        public override void ShutUp()
        {
            Console.WriteLine("{0} stopped talking", this.Name);
        }
    }

    public class PersonRepository
    {
        public PersonBase Find(string name)
        {
            if (name.Contains("Old"))
            {
                return new OldPerson();
            }
            else if (name.Contains("Young"))
            {
                return new YoungPerson();
            }

            return PersonBase.Null;
        }
    }
}

As expected, the find to a ‘RestlessPerson’ gets a null object returned and does nothing.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值