Null Object Design Pattern

null object design pattern image

A Null Object is an object with defined neutral ("null") behavior. The Null Object design pattern describes the uses of such objects and their behavior (or lack thereof). (Wikipedia)

  • AbstractClass defines abstract primitive operations for client’s collaborator that concrete implementations have to define. It optionally implements default behavior for the interface common to all classes, as appropriate.
  • RealClass implements AbstractClass performing some real actions that client expects.
  • NullClass implements AbstractClass and does nothing in order to provide a non-null object to the client that can be substituted for a real object
  • Client has a reference to abstract class implementation and collaborates with it and does not care if the implementation is a null object or an real object.

If some clients expect the null object to do nothing one way and some another, multiple NullObject classes will be required. If the do nothing behavior must be customized at run time. A Null Object does not transform to become a Real Object. If the object may decide to stop providing do nothing behavior and start providing real behavior, it is not a null object. The null behavior is not designed to be mixed into an object that needs some do nothing behavior. It is designed for a class which delegates to a collaborator all of the behavior that may or may not be do nothing behavior.

  • The Null Object Pattern is used to avoid special if blocks for do nothing code, by putting the “do nothing” code in the Null Object which becomes responsible for doing nothing. The client is not aware anymore if the real object or the null object is called so the ' if ' section is removed from client implementation.
  • Some collaborator instances should do nothing
  • Can  be used to act as a stub for testing
  • Can be used to remove old functionality by replacing it with null objects, as a result the existing code does not need to be touched.

  • A Null Object is predictable and has no side effects: it does nothing.
  • Provides intelligent do nothing behavior and hiding the details from its collaborators
  • Provide an object as a surrogate for the lack of an object of a given type.
  •  Clients can treat real collaborators and null collaborators uniformly.

  • Can make errors/bugs appear as normal program execution.
  • Can be difficult to implement if various clients do not agree on how the null object should do nothing as when your AbstractObject interface is not well defined.

Sample Code

Related Patterns
  • Null Object pattern can be regarded as a special case of the State and the Strategy pattern.
  • Null Object  pattern is more likely to be used in conjunction with the Factory Method pattern. 
  • Null Object class is often implemented as a Singleton since a null object usually does not have any state.
  • Null Object can be used to allow a Visitor to safely visit a hierarchy and handle the null situation.
  • The use of a Null Object can be similar to that of a Proxy, but the two patterns have different purposes. A proxy provides a level of indirection when accessing a real subject, thus controlling access to the subject. A null collaborator does not hide a real object and control access to it, it replaces the real object. A proxy may eventually mutate to start acting like a real subject. A null object will not mutate to start providing real behavior, it will always provide do nothing behavior.


Further readings


  1. Machine Learning Projects for Final Year machine learning projects for final year

    Deep Learning Projects assist final year students with improving your applied Deep Learning skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include Deep Learning projects for final year into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Deep Learning Projects for Final Year even arrange a more significant compensation.

    Python Training in Chennai Python Training in Chennai Angular Training Project Centers in Chennai

  2. Ucuz, kaliteli ve organik sosyal medya hizmetleri satın almak için Ravje Medyayı tercih edebilir ve sosyal medya hesaplarını hızla büyütebilirsin. Ravje Medya ile sosyal medya hesaplarını organik ve gerçek kişiler ile geliştirebilir, kişisel ya da ticari hesapların için Ravje Medyayı tercih edebilirsin. Ravje Medya internet sitesine giriş yapmak için hemen tıkla:

    İnstagram takipçi satın almak için Ravje Medya hizmetlerini tercih edebilir, güvenilir ve gerçek takipçilere Ravje Medya ile ulaşabilirsin. İnstagram takipçi satın almak artık Ravje Medya ile oldukça güvenilir. Hemen instagram takipçi satın almak için Ravje Medyanın ilgili sayfasını ziyaret et: instagram takipçi satın al

    Tiktok takipçi satın al istiyorsan tercihini Ravje Medya yap! Ravje Medya uzman kadrosu ve profesyonel ekibi ile sizlere Tiktok takipçi satın alma hizmetide sunmaktadır. Tiktok takipçi satın almak için hemen tıkla: tiktok takipçi satın al

    İnstagram beğeni satın almak için Ravje medya instagram beğeni satın al sayfasına giriş yap, hızlı ve kaliteli instagram beğeni satın al: instagram beğeni satın al

    Youtube izlenme satın al sayfası ile hemen youtube izlenme satın al! Ravje medya kalitesi ile hemen youtube izlenme satın almak için tıklayın: youtube izlenme satın al

    Twitter takipçi satın almak istiyorsan Ravje medya twitter takipçi satın al sayfasına tıkla, Ravje medya güvencesi ile organik twitter takipçi satın al: twitter takipçi satın al


Post a Comment

Popular Posts