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
- The Null Object Pattern solves the problems that may be caused by using null values in a system, such as the NullPointerException exception.
- It allows the system to continue running using a "safe" null object when there is no suitable object, instead of failing.
Use Cases
- When the system needs to handle null objects but wants to avoid null checks or handling null values.
Implementation
- Define the protocol: Define a protocol or interface that specifies the behavior to be implemented.
- Create the concrete object: The concrete object that implements the protocol, providing actual behavior.
- Create the null object: Also implements the same protocol but provides an "empty" implementation, i.e., performs no meaningful operation.
Key code
- Protocol or interface: Specifies the methods an object must implement.
- Concrete object: Implements the protocol and contains actual business logic.
- Null object: Implements the protocol, but the method implementations are empty or default behavior.
Application Example
- Logging system: In a logging system, a null object may represent a logger that does nothing.
- Default user: In a system, if there is currently no user, a null user object can be used instead of null.
Advantages
- Avoid null checks: Eliminates null value checks in the code.
- Simplifies client code: The client can call methods directly regardless of whether the object is null.
- Extensibility: Adding new concrete objects is transparent to the client, with no need to modify existing code.
Disadvantages
- May hide errors: Using null objects may hide errors or exceptional conditions, making debugging difficult.
- Increases design complexity: A null object needs to be implemented for every interface that may return null.
Usage recommendation
- When the system needs to handle null values but wants to simplify error handling and avoid null checks, consider using the Null Object Pattern.
Notes
- The Null Object Pattern should be used with caution, ensuring it does not mask errors or hide the system's true state.
- A null object should provide the same interface as the concrete object, so that client code can use it without changes.
structures
The Null Object Pattern contains the following main roles:
-
Abstract Object (Abstract Object): Defines the interface expected by the client. This interface can be an abstract class or an interface.
-
Concrete Object (Concrete Object): The concrete classes that implement the abstract object interface. These classes provide real behavior.
-
Null Object (Null Object): The null object class that implements the abstract object interface. This class provides default invalid behavior for use when an object is unavailable or not available. It can serve as a substitute for the concrete object, replacing null checks in client code.
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