Object Null Design Pattern

Source: Internet
Author: User

Object Null design pattern: Used when it is frequently necessary to determine whether an object is empty.

Using the object Null design pattern facilitates management, initial assignment problems.

Concrete implementation: You can specifically design a single case class, specifically to assign an initial value to the object.

Reproduced below is an article on the web that describes the object null pattern:

      in the design implementation of Esframework, the null object design pattern is used in many places. The meaning of the Null object pattern is to provide an object to the specified type in place of the null object. Null object provides a "do nothing" behavior, hiding the details from its collaborators.
   for how to understand and apply this pattern, an example can be well explained. In this section we are discussing the message dispatcher, which uses the aforementioned logger and injects the specific logger object through the attribute.          Private  IEsfLogger esfLogger;
         public  iesflogger esflogger
         {
              Set
            {
                  this .esflogger  =  value ;
            }
        }    

     Now suppose that we use the logger for logging in multiple places within the message dispatcher, we always write such statements:      if   (this .esflogger !=   null)
     {
            this. EsfLogger.Log (); //record log
     }

In other words, before using, we have to determine whether the logger's reference is empty, and if it is not NULL, it can call its log method. If there is a lot of logging in the call logger, then every place is flooded with code that determines whether its reference is empty. There is no way to avoid all of this judgment statement anymore, there is. That is to use the null object design pattern.
Esframework provides the corresponding null object type for each of the necessary components, which are prefixed with "Empty". For example, Iesflogger's corresponding null object type is the Emptyesflogger,emptyesflogger-implemented Log method that does nothing: public void Log (string errortype, String msg, string location, ERRORLEVEL level)
{
Do nothing!
}

With Emptyesflogger, we can design the logger properties of the message dispatcher like this: private Iesflogger Esflogger = new Emptyesflogger ();
Public Iesflogger Esflogger
{
Set
{
if (value!= null)
{
this. Esflogger = value?? New Emptyesflogger ();
}
}
}

First, set the default value of the Esflogger field to a null Object. Second, each time the caller attempts to set the Esflogger property to NULL, a null object is also assigned to the field.
In this way, within the message allocator, we can easily use the logger directly, without having to judge whether the reference is empty, because it always points to a valid object, even if it is null object.

In addition to common assembly assemblies that use the null object pattern, there is an occasion that is ideal for using the null object pattern, that is, "events." Do you remember that every time we trigger an event we need to determine whether it is empty, which is trivial, and we can still simplify it by using the null object pattern. For example, an event is defined in a class: public event Cbsimple someoneconnected;

In the constructor of a class, you can initialize it by using null object: this. someoneconnected + = delegate {};

This way, each time an event is triggered, it is no longer necessary to determine whether it is null:


Note: This article extracts the manuscript of the self. The design, implementation and application of the net Communication framework
this.  Someoneconnected (); No need to judge if NULL, triggering events directly

The flexibility to use the null object design pattern can make our code more concise and refined.

Go to: http://www.cnblogs.com/zhuweisky/archive/2007/02/28/660075.html

Here is a discussion of the object null design pattern of the post, the discussion is very thorough, do not understand the object null design pattern, you can refer to, after reading I believe you can understand its usefulness and when adopted, hehe.

http://www.javaeye.com/topic/272872

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.