Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Null Object Pattern

Null Object Pattern

In the Null Object Pattern (Null Object Pattern), a null object replaces the check for NULL object instances. Instead of checking for null values, a Null object reflects a relationship that does nothing. Such a Null object can also provide default behavior when data is unavailable.

In the Null Object Pattern, we create an abstract class that specifies the various operations to be performed and entity classes that extend it, and also create a null object class that makes no implementation of that class, which will be used seamlessly wherever a null check is needed.


Summary

Intent

Use a null object in place of a null value; this null object implements the same interface but performs no operation or provides a default operation for requests.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Avoid null checks: Eliminates null value checks in the code.
  2. Simplifies client code: The client can call methods directly regardless of whether the object is null.
  3. Extensibility: Adding new concrete objects is transparent to the client, with no need to modify existing code.

Disadvantages

Usage recommendation

Notes

structures

The Null Object Pattern contains the following main roles:

Create a class that implements

We will create an abstract class that defines an operation (in this case, the customer's name) AbstractCustomer abstract class, and entity classes that extend the AbstractCustomer class. The factory class CustomerFactory returns based on the name passed by the customer RealCustomer or NullCustomer object.

NullPatternDemo, and our demo class uses the CustomerFactory to demonstrate the use of the Null Object Pattern.

Step 1

Create an abstract class.

AbstractCustomer.java

public

abstract

class

AbstractCustomer

{

protected

String

name

;

public

abstract

boolean

isNil

(

)

;

public

abstract

String

getName

(

)

;

}

Step 2

Create the entity classes that extend the 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

;

}

}

Step 3

creation CustomerFactory class.

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

(

)

;

}

}

Step 4

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

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

(

)

)

;

}

}

Step 5

Run the program, output result:



Customers

Rob

Not Available in Customer Database

Julie

Not Available in Customer Database