mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +00:00
docs: update null object
This commit is contained in:
+43
-17
@@ -3,24 +3,25 @@ title: Null Object
|
||||
category: Behavioral
|
||||
language: en
|
||||
tag:
|
||||
- Extensibility
|
||||
- Code simplification
|
||||
- Decoupling
|
||||
- Polymorphism
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Active Nothing
|
||||
* Stub
|
||||
|
||||
## Intent
|
||||
|
||||
In most object-oriented languages, such as Java or C#, references may be null. These references need
|
||||
to be checked to ensure they are not null before invoking any methods, because methods typically
|
||||
cannot be invoked on null references. Instead of using a null reference to convey absence of an
|
||||
object (for instance, a non-existent customer), one uses an object which implements the expected
|
||||
interface, but whose method body is empty. The advantage of this approach over a working default
|
||||
implementation is that a Null Object is very predictable and has no side effects: it does nothing.
|
||||
To provide a default behavior for an object, avoiding the need for null checks and enhancing code readability.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> We are building a binary tree from nodes. There are ordinary nodes and "empty" nodes. Traversing
|
||||
> the tree normally should not cause errors, so we use null object pattern where necessary.
|
||||
> We are building a binary tree from nodes. There are ordinary nodes and "empty" nodes. Traversing the tree normally should not cause errors, so we use null object pattern where necessary.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -28,9 +29,7 @@ In plain words
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In object-oriented computer programming, a null object is an object with no referenced value or
|
||||
> with defined neutral ("null") behavior. The null object design pattern describes the uses of such
|
||||
> objects and their behavior (or lack thereof).
|
||||
> In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior. The null object design pattern describes the uses of such objects and their behavior (or lack thereof).
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
@@ -51,8 +50,7 @@ public interface Node {
|
||||
}
|
||||
```
|
||||
|
||||
We have two implementations of `Node`. The normal implementation `NodeImpl` and `NullNode` for
|
||||
empty nodes.
|
||||
We have two implementations of `Node`. The normal implementation `NodeImpl` and `NullNode` for empty nodes.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@@ -169,15 +167,43 @@ Program output:
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Null Object pattern when
|
||||
* When you need to provide a default behavior in place of a null object.
|
||||
* To simplify the client code by eliminating null checks.
|
||||
* When a default action is preferable to handling a null reference.
|
||||
|
||||
* You want to avoid explicit null checks and keep the algorithm elegant and easy to read.
|
||||
## Known Uses
|
||||
|
||||
* Logging systems where a NullLogger can be used to avoid null checks.
|
||||
* Collections that use a NullIterator to handle empty collections gracefully.
|
||||
* GUI systems where a NullComponent can be used to represent a component that does nothing.
|
||||
|
||||
## Consequences
|
||||
|
||||
Benefits:
|
||||
|
||||
* Eliminates the need for null checks, reducing the risk of NullPointerException.
|
||||
* Simplifies the client code and enhances readability.
|
||||
* Promotes the use of polymorphism by handling default behavior through a common interface.
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* May introduce additional classes, potentially increasing the overall complexity of the system.
|
||||
* The default behavior might mask potential issues that would otherwise be caught by explicit null handling.
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Null Object can be seen as a special case of the Strategy Pattern where the strategy is to do nothing.
|
||||
* [State](https://java-design-patterns.com/patterns/state/): Similar in that both patterns can handle different states or behaviors; Null Object is like a state that does nothing.
|
||||
* [Factory](https://java-design-patterns.com/patterns/factory/): Often used to provide Null Objects in place of actual objects.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Pattern Languages of Program Design 3](https://www.amazon.com/gp/product/0201310112/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201310112&linkCode=as2&tag=javadesignpat-20&linkId=7372ffb8a4e39a3bb10f199b89aef921)
|
||||
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||
* [Refactoring: Improving the Design of Existing Code](https://amzn.to/3UJ7etA)
|
||||
|
||||
Reference in New Issue
Block a user