HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Empty object model (Null Object Pattern), and an empty object substituted check for NULL object instance. Null objects are not check for null values, but not the reaction a relationship of any action. Such Null objects can also provide default behavior when data is not available.
Empty object model, we create a designated entity of the abstract class and extend this class to perform a variety of operations, but also create a class did not do any of this empty object class implements the empty object class seamlessly use in place need to check for null values.
We will create a custom action (in this case, the customer's name)AbstractCustomerabstract class, and extends the entity classesAbstractCustomerclass.CustomerFactoryfactory class name based on the customer to deliver or returnRealCustomerNullCustomer object.
NullPatternDemo,we demonstrate the use ofCustomerFactoryclass to demonstrate the use of an empty object model.
Create an abstract class.
AbstractCustomer.java
public abstract class AbstractCustomer { protected String name; public abstract boolean isNil (); public abstract String getName (); }
Create extends the entity classes above class.
RealCustomer.java
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
public class NullCustomer extends AbstractCustomer { @Override public String getName () { return "Not Available in Customer Database"; } @Override public boolean isNil () { return true; } }
CreateCustomerFactoryclass.
CustomerFactory.java
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 (); } }
UseCustomerFactory,based on the client's name to pass to getRealCustomerorNullCustomerobject.
NullPatternDemo.java
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 ()); } }
Verify output.
Customers Rob Not Available in Customer Database Julie Not Available in Customer Database