Null Object Pattern

This article is excerpted from "design patterns should be learned this way"

1 definition of empty object mode

Null Object Pattern does not belong to GoF design pattern, but as a frequent pattern, it is enough to be regarded as design pattern. Its specific definition is to design an empty object to replace the check of NULL object instances. NULL objects do not check control, but reflect a relationship that does nothing. Such NULL objects can also provide default behavior when data is not available, which belongs to behavioral design pattern.

The null object provides intelligent do nothing behavior, hiding the details from its collaborators

2. Application scenario of empty object mode

The empty object mode is applicable to the following application scenarios.

(1) Object instance requires a collaboration instance. The empty object pattern does not introduce collaboration instances, it just uses existing collaboration instances.

(2) Some collaboration instances do not require any processing.

(3) Abstract the code logic that the object instance does not exist from the client.

3 UML class diagram of empty object pattern

The UML class diagram of empty object pattern is shown in the following figure.

As can be seen from the above figure, the empty object mode mainly contains three roles.

(1) Abstract object: defines the behaviors and properties common to all subclasses.

(2) Real object: inherits the AbstractObject class and implements all behaviors.

(3) Null object: inherits the AbstractObject class and does not implement or assign values to the parent class methods and properties.

4 general writing method of empty object mode

The following is a common way to write empty object mode.

public class Client {

    public static void main(String[] args) {
        ObjectFactory factory = new ObjectFactory();
        System.out.println(factory.getObject("Joe").isNill());
        System.out.println(factory.getObject("Tom").isNill());
    }


    //Abstract object
    static abstract class AbstractObject{
        abstract void request();
        abstract boolean isNill();
    }

    //Empty object
    static class NullObject extends AbstractObject{

        public void request() {
            System.out.println("Not Available Request");
        }

        boolean isNill() {
            return true;
        }
    }

    //Real object
    static class RealObject extends AbstractObject{
        private String name;
        public RealObject(String name) {
            this.name = name;
        }

        public void request() {
            System.out.println("Do samething...");
        }

        boolean isNill() {
            return false;
        }
    }

    //Object factory
    static class ObjectFactory{
        private static final String[] names = {"Tom","Mic","James"};

        public AbstractObject getObject(String name){
            for (String n : names) {
                if(n.equalsIgnoreCase(name)){
                    return new RealObject(name);
                }
            }
            return new NullObject();
        }
    }
}

5 advantages of empty object mode

(1) It can strengthen the stability of the system, effectively reduce the impact of null pointer error reporting on the whole system, and make the system more stable.

(2) It can realize the customized control of empty objects and master the initiative to deal with empty objects.

(3) It does not rely on the Client to ensure the stable operation of the whole system.

(4) It is more elegant and understandable by defining isNull() to replace the conditional statement = = null.

6 disadvantages of empty object mode

For each real entity to be returned, a corresponding empty object model should be established, which will increase the number of classes.

Focus on WeChat's official account of Tom architecture and reply to the "design pattern" to get the complete source code.

[recommendation] Tom bomb architecture: 30 real cases of design patterns (with source code attached). Challenging the annual salary of 60W is not a dream

This article is the original of "Tom bomb architecture". Please indicate the source for reprint. Technology lies in sharing, I share my happiness!
If this article is helpful to you, you are welcome to pay attention and praise; If you have any suggestions, you can also leave comments or private letters. Your support is the driving force for me to adhere to my creation. Focus on WeChat official account Tom structure, get more dry cargo!

Tags: Java Design Pattern architecture

Posted on Fri, 26 Nov 2021 08:51:02 -0500 by menwn