Skip to content

Commit bc35911

Browse files
committed
Update README.md
1 parent b5c6a89 commit bc35911

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

null-object/README.md

+30-18
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,34 @@ tags:
99
---
1010

1111
## Intent
12-
In most object-oriented languages, such as Java or C#, references
13-
may be null. These references need to be checked to ensure they are not null
14-
before invoking any methods, because methods typically cannot be invoked on
15-
null references. Instead of using a null reference to convey absence of an
16-
object (for instance, a non-existent customer), one uses an object which
17-
implements the expected interface, but whose method body is empty. The
18-
advantage of this approach over a working default implementation is that a Null
19-
Object is very predictable and has no side effects: it does nothing.
12+
13+
In most object-oriented languages, such as Java or C#, references may be null. These references need
14+
to be checked to ensure they are not null before invoking any methods, because methods typically
15+
cannot be invoked on null references. Instead of using a null reference to convey absence of an
16+
object (for instance, a non-existent customer), one uses an object which implements the expected
17+
interface, but whose method body is empty. The advantage of this approach over a working default
18+
implementation is that a Null Object is very predictable and has no side effects: it does nothing.
2019

2120
## Explanation
2221

2322
Real world example
2423

25-
> 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.
24+
> We are building a binary tree from nodes. There are ordinary nodes and "empty" nodes. Traversing
25+
> the tree normally should not cause errors, so we use null object pattern where necessary.
2626
2727
In plain words
2828

2929
> Null Object pattern handles "empty" objects gracefully.
3030
3131
Wikipedia says
3232

33-
> 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).
33+
> In object-oriented computer programming, a null object is an object with no referenced value or
34+
> with defined neutral ("null") behavior. The null object design pattern describes the uses of such
35+
> objects and their behavior (or lack thereof).
3436
3537
**Programmatic Example**
3638

37-
Here's the definitions for node interface and its implementations.
39+
Here's the definition of `Node` interface.
3840

3941
```java
4042
public interface Node {
@@ -49,7 +51,12 @@ public interface Node {
4951

5052
void walk();
5153
}
54+
```
55+
56+
We have two implementations of `Node`. The normal implementation `NodeImpl` and `NullNode` for
57+
empty nodes.
5258

59+
```java
5360
public class NodeImpl implements Node {
5461

5562
private static final Logger LOGGER = LoggerFactory.getLogger(NodeImpl.class);
@@ -135,7 +142,6 @@ public final class NullNode implements Node {
135142
// Do nothing
136143
}
137144
}
138-
139145
```
140146

141147
Then we can construct and traverse the binary tree without errors as follows.
@@ -152,18 +158,24 @@ Then we can construct and traverse the binary tree without errors as follows.
152158
)
153159
);
154160
root.walk();
155-
156-
// 1
157-
// 11
158-
// 111
159-
// 12
160-
// 122
161+
```
162+
163+
Program output:
164+
165+
```
166+
1
167+
11
168+
111
169+
12
170+
122
161171
```
162172

163173
## Class diagram
174+
164175
![alt text](./etc/null-object.png "Null Object")
165176

166177
## Applicability
178+
167179
Use the Null Object pattern when
168180

169181
* You want to avoid explicit null checks and keep the algorithm elegant and easy to read.

0 commit comments

Comments
 (0)