empty object mode
In the Null Object Pattern, an empty object replaces the check of a NULL object instance. Null objects do not check for null values, but instead react to 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 the various operations to be performed and an entity class that extends the class, and also creates an empty object class that does not implement any implementation of the class. Use seamlessly where you need to check for null values.
We will create an AbstractCustomer abstract class that defines the operation (here, the name of the client), and an entity class that extends the AbstractCustomer class. The factory class CustomerFactory returns the RealCustomer or NullCustomer object based on the name passed by the client.
NullPatternDemo, our demo class uses CustomerFactory to demonstrate the use of empty object patterns.
Create an abstract class.
Create an entity class that extends the above classes.
Create the CustomerFactory class.
Use CustomerFactory to get the RealCustomer or NullCustomer object based on the name passed by the client.
Executing the program, outputting results:
Customers Rob Not Available in Customer Database Julie Not Available in Customer Database