This article is excerpted from This is How Design Patterns should Be Learned

1 Definition of empty object schema

The Null Object Pattern is not a GoF design Pattern, but it is a Pattern that occurs frequently enough to be considered a design Pattern. This is defined as a check that designs an empty object instead of a NULL object instance. A NULL object is not a check control, but rather reflects a relationship that does nothing. Such NULL objects can also provide default behavior when data is not available, which falls within the behavioral design pattern.

The original: Provide an object as a surrogate for the lack of an object of a given type. The Null object provides intelligent do nothing behavior, hiding the details from its collaborators.

2 Application scenarios of the empty object mode

The empty object mode applies to the following application scenarios.

(1) Object instances require a collaboration instance. The empty object pattern does not introduce collaboration instances; it simply uses existing collaboration instances.

(2) Some collaboration instances do not need to do any processing.

(3) From the client to the object instance does not exist in the code logic abstraction.

UML class diagram for empty object schema

The UML class diagram for the empty object pattern is shown below.

As you can see from the figure above, the empty object pattern mainly contains three roles.

(1) AbstractObject: define the behavior and attributes common to all subclasses.

(2) RealObject: Inherit the AbstractObject class and implement all behaviors.

NullObject: Inherit the AbstractObject class. Do not implement and assign values to the parent class methods and attributes.

A generic way to write the empty object schema

The following is a common way to write the empty object schema.


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 objects
    static abstract class AbstractObject{
        abstract void request(a);
        abstract boolean isNill(a);
    }

    / / null objects
    static class NullObject extends AbstractObject{

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

        boolean isNill(a) {
            return true; }}// Real objects
    static class RealObject extends AbstractObject{
        private String name;
        public RealObject(String name) {
            this.name = name;
        }

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

        boolean isNill(a) {
            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 newRealObject(name); }}return newNullObject(); }}}Copy the code

5 Advantages of the empty object pattern

(1) It can strengthen the stability of the system, can effectively reduce the impact of null pointer error on the whole system, so that the system is more stable.

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

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

(4) It is more elegant and easy to understand by defining isNull() as an alternative to the conditional statement ==null.

6 Disadvantages of the empty object pattern

Each real entity to be returned has a corresponding empty object model, which increases the number of classes.

Tom play architecture: 30 real cases of design patterns (attached source code), the challenge of annual salary 60W is not a dream

This article is “Tom play structure” original, reproduced please indicate the source. Technology is to share, I share my happiness! If this article is helpful to you, welcome to follow and like; If you have any suggestions can also leave a comment or private letter, your support is my motivation to adhere to the creation. Pay attention to “Tom bomb architecture” for more technical dry goods!