Skip to content

Commit e34de39

Browse files
committed
iluwatar#590 explanation for Null Object
1 parent 4d95d38 commit e34de39

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

null-object/README.md

+137
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,143 @@ implements the expected interface, but whose method body is empty. The
1818
advantage of this approach over a working default implementation is that a Null
1919
Object is very predictable and has no side effects: it does nothing.
2020

21+
## Explanation
22+
23+
Real world example
24+
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.
26+
27+
In plain words
28+
29+
> Null Object pattern handles "empty" objects gracefully.
30+
31+
Wikipedia says
32+
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).
34+
35+
**Programmatic Example**
36+
37+
Here's the definitions for node interface and its implementations.
38+
39+
```java
40+
public interface Node {
41+
42+
String getName();
43+
44+
int getTreeSize();
45+
46+
Node getLeft();
47+
48+
Node getRight();
49+
50+
void walk();
51+
}
52+
53+
public class NodeImpl implements Node {
54+
55+
private static final Logger LOGGER = LoggerFactory.getLogger(NodeImpl.class);
56+
57+
private final String name;
58+
private final Node left;
59+
private final Node right;
60+
61+
/**
62+
* Constructor.
63+
*/
64+
public NodeImpl(String name, Node left, Node right) {
65+
this.name = name;
66+
this.left = left;
67+
this.right = right;
68+
}
69+
70+
@Override
71+
public int getTreeSize() {
72+
return 1 + left.getTreeSize() + right.getTreeSize();
73+
}
74+
75+
@Override
76+
public Node getLeft() {
77+
return left;
78+
}
79+
80+
@Override
81+
public Node getRight() {
82+
return right;
83+
}
84+
85+
@Override
86+
public String getName() {
87+
return name;
88+
}
89+
90+
@Override
91+
public void walk() {
92+
LOGGER.info(name);
93+
if (left.getTreeSize() > 0) {
94+
left.walk();
95+
}
96+
if (right.getTreeSize() > 0) {
97+
right.walk();
98+
}
99+
}
100+
}
101+
102+
public final class NullNode implements Node {
103+
104+
private static NullNode instance = new NullNode();
105+
106+
private NullNode() {
107+
}
108+
109+
public static NullNode getInstance() {
110+
return instance;
111+
}
112+
113+
@Override
114+
public int getTreeSize() {
115+
return 0;
116+
}
117+
118+
@Override
119+
public Node getLeft() {
120+
return null;
121+
}
122+
123+
@Override
124+
public Node getRight() {
125+
return null;
126+
}
127+
128+
@Override
129+
public String getName() {
130+
return null;
131+
}
132+
133+
@Override
134+
public void walk() {
135+
// Do nothing
136+
}
137+
}
138+
139+
```
140+
141+
Then we can construct and traverse the binary tree without errors as follows.
142+
143+
```java
144+
Node root =
145+
new NodeImpl("1", new NodeImpl("11", new NodeImpl("111", NullNode.getInstance(),
146+
NullNode.getInstance()), NullNode.getInstance()), new NodeImpl("12",
147+
NullNode.getInstance(), new NodeImpl("122", NullNode.getInstance(),
148+
NullNode.getInstance())));
149+
root.walk();
150+
151+
// 1
152+
// 11
153+
// 111
154+
// 12
155+
// 122
156+
```
157+
21158
## Class diagram
22159
![alt text](./etc/null-object.png "Null Object")
23160

0 commit comments

Comments
 (0)