6.24. Null object mode

发布时间 :2025-10-25 12:24:48 UTC      

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.

6.24.1. Realize

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.

空对象模式的 UML 图

6.24.2. Step 1

Create an abstract class.

AbstractCustomer.java

publicabstractclassAbstractCustomer{protectedStringname;publicabstractbooleanisNil();publicabstractStringgetName();}

6.24.3. Step 2

Create an entity class that extends the above class.

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

Create CustomerFactory Class.

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

Use CustomerFactory Based on the name passed by the customer, to get RealCustomer Or NullCustomer Object.

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

Execute the program and output the result:

Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.