Java single application - Architecture Mode - 03. Design mode - 22. Empty object mode

Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-null-object-pattern.html
More tutorials: Beam cloud - free course

Empty object mode

Serial number Chapter in text video
1 Summary -
2 Realization -

Please refer to the navigation above for reading

1. overview

In Null Object Pattern, an empty object replaces the check of a Null object instance. A Null object does not check for Null values, but reflects a relationship that does nothing. Such Null objects can also provide default behavior when data is not available.

In the empty object mode, we create an abstract class that specifies various operations to be performed, an entity class that extends the class, and an empty object class that does not have any implementation for the class. The empty object class will be used seamlessly where null values need to be checked.

2. implementation

We will create an AbstractCustomer abstract class that defines the operation (in this case, the name of the customer), and an entity class that extends the AbstractCustomer class.

The factory class CustomerFactory returns a RealCustomer or NullCustomer object based on the name passed by the customer.

NullPatternDemo, our demo class uses CustomerFactory to demonstrate the use of empty object patterns.

Step 1

Create an abstract class.

AbstractCustomer.java, the code is as follows:

public abstract class AbstractCustomer {
   protected String name;
   public abstract boolean isNil();
   public abstract String getName();
}

Step 2

Create entity classes that extend the above classes.

RealCustomer.java, the code is as follows:

public class RealCustomer extends AbstractCustomer {

   public RealCustomer(String name) {
      this.name = name;    
   }

   @Override
   public String getName() {
      return name;
   }

   @Override
   public boolean isNil() {
      return false;
   }
}

NullCustomer.java, the code is as follows:

public class NullCustomer extends AbstractCustomer {

   @Override
   public String getName() {
      return "Not Available in Customer Database";
   }

   @Override
   public boolean isNil() {
      return true;
   }
}

Step 3

Create the CustomerFactory class.

CustomerFactory.java, the code is as follows:

public class CustomerFactory {

   public static final String[] names = {"Rob", "Joe", "Julie"};

   public static AbstractCustomer getCustomer(String name){
      for (int i = 0; i < names.length; i++) {
         if (names[i].equalsIgnoreCase(name)){
            return new RealCustomer(name);
         }
      }
      return new NullCustomer();
   }
}

Step 4

Use CustomerFactory to get the RealCustomer or NullCustomer object based on the name passed by the customer.

NullPatternDemo.java, the code is as follows:

public class NullPatternDemo {
   public static void main(String[] args) {

      AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob");
      AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob");
      AbstractCustomer customer3 = CustomerFactory.getCustomer("Julie");
      AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura");

      System.out.println("Customers");
      System.out.println(customer1.getName());
      System.out.println(customer2.getName());
      System.out.println(customer3.getName());
      System.out.println(customer4.getName());
   }
}

Step 5

Execute the program and output the result:

Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database

Last article: State mode
Next article: Strategy mode

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.


Tags: Java Database

Posted on Fri, 13 Mar 2020 07:50:04 -0700 by khf79