You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: null-object/README.md
+30-18
Original file line number
Diff line number
Diff line change
@@ -9,32 +9,34 @@ tags:
9
9
---
10
10
11
11
## 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.
20
19
21
20
## Explanation
22
21
23
22
Real world example
24
23
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.
> 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).
34
36
35
37
**Programmatic Example**
36
38
37
-
Here's the definitions for node interface and its implementations.
39
+
Here's the definition of `Node` interface.
38
40
39
41
```java
40
42
publicinterfaceNode {
@@ -49,7 +51,12 @@ public interface Node {
49
51
50
52
voidwalk();
51
53
}
54
+
```
55
+
56
+
We have two implementations of `Node`. The normal implementation `NodeImpl` and `NullNode` for
0 commit comments