In the null object schema (Null Object Pattern), an empty object replaces the check of the NULL object instance. Instead of checking for null values, the Null object reflects a relationship that does nothing. Such Null objects can also provide default behavior when data is not available.
In the empty object pattern, we create an abstract class that specifies various operations to be performed and an entity class that extends it, as well as an empty object class that has not been implemented at all. The empty object class will be used seamlessly where null values need to be checked. We will create a definition operation (in this case, the name of the customer) AbstractCustomer Abstract classes, and extend the AbstractCustomer The entity class of the. Factory class CustomerFactory Return based on the name passed by the customer RealCustomer Or NullCustomer Object. NullPatternDemo Our demo class uses the CustomerFactory To demonstrate the use of the empty object pattern. Create an abstract class. Create an entity class that extends the above class. Create CustomerFactory Class. Use CustomerFactory Based on the name passed by the customer, to get RealCustomer Or NullCustomer Object. Execute the program and output the result: 6.24.1. Realize ¶

6.24.2. Step 1 ¶
AbstractCustomer.java ¶
publicabstractclassAbstractCustomer{protectedStringname;publicabstractbooleanisNil();publicabstractStringgetName();}
6.24.3. Step 2 ¶
RealCustomer.java ¶
publicclassRealCustomerextendsAbstractCustomer{publicRealCustomer(Stringname){this.name=name;}@OverridepublicStringgetName(){returnname;}@OverridepublicbooleanisNil(){returnfalse;}}
NullCustomer.java ¶
publicclassNullCustomerextendsAbstractCustomer{@OverridepublicStringgetName(){return"Not
Available in Customer
Database";}@OverridepublicbooleanisNil(){returntrue;}}
6.24.4. Step 3 ¶
CustomerFactory.java ¶
publicclassCustomerFactory{publicstaticfinalString[]names={"Rob","Joe","Julie"};publicstaticAbstractCustomergetCustomer(Stringname){for(inti=0;i<names.length;i++){if(names[i].equalsIgnoreCase(name)){returnnewRealCustomer(name);}}returnnewNullCustomer();}}
6.24.5. Step 4 ¶
NullPatternDemo.java ¶
publicclassNullPatternDemo{publicstaticvoidmain(String[]args){AbstractCustomercustomer1=CustomerFactory.getCustomer("Rob");AbstractCustomercustomer2=CustomerFactory.getCustomer("Bob");AbstractCustomercustomer3=CustomerFactory.getCustomer("Julie");AbstractCustomercustomer4=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());}}
6.24.6. Step 5 ¶
Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database