Skip to content

Commit 1cad280

Browse files
committed
Added Null Object pattern.
1 parent eb43f6e commit 1cad280

File tree

9 files changed

+225
-4
lines changed

9 files changed

+225
-4
lines changed

null-object/etc/test.png

16.3 KB
Loading

null-object/etc/test.ucls

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4+
<interface id="1" language="java" name="com.iluwatar.Node" project="null-object"
5+
file="/null-object/src/main/java/com/iluwatar/Node.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="535" y="139"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</interface>
13+
<class id="2" language="java" name="com.iluwatar.NodeImpl" project="null-object"
14+
file="/null-object/src/main/java/com/iluwatar/NodeImpl.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="857" y="348"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<class id="3" language="java" name="com.iluwatar.NullNode" project="null-object"
23+
file="/null-object/src/main/java/com/iluwatar/NullNode.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="535" y="408"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<association id="4">
32+
<end type="SOURCE" refId="2" navigable="false">
33+
<attribute id="5" name="left"/>
34+
<multiplicity id="6" minimum="0" maximum="1"/>
35+
</end>
36+
<end type="TARGET" refId="1" navigable="true"/>
37+
<display labels="true" multiplicity="true"/>
38+
</association>
39+
<association id="7">
40+
<end type="SOURCE" refId="2" navigable="false">
41+
<attribute id="8" name="right"/>
42+
<multiplicity id="9" minimum="0" maximum="1"/>
43+
</end>
44+
<end type="TARGET" refId="1" navigable="true"/>
45+
<display labels="true" multiplicity="true"/>
46+
</association>
47+
<realization id="10">
48+
<end type="SOURCE" refId="2"/>
49+
<end type="TARGET" refId="1"/>
50+
</realization>
51+
<realization id="11">
52+
<end type="SOURCE" refId="3"/>
53+
<end type="TARGET" refId="1"/>
54+
</realization>
55+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
56+
sort-features="false" accessors="true" visibility="true">
57+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
58+
<operations public="true" package="true" protected="true" private="true" static="true"/>
59+
</classifier-display>
60+
<association-display labels="true" multiplicity="true"/>
61+
</class-diagram>

null-object/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>null-object</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Null Object pattern replaces null values with neutral objects.
6+
* Many times this simplifies algorithms since no extra null checks
7+
* are needed.
8+
*
9+
* In this example we build a binary tree where the nodes are either
10+
* normal or Null Objects. No null values are used in the tree making
11+
* the traversal easy.
12+
*
13+
*/
14+
public class App
15+
{
16+
public static void main( String[] args ) {
17+
18+
Node root = new NodeImpl("1",
19+
new NodeImpl("11",
20+
new NodeImpl("111",
21+
new NullNode(),
22+
new NullNode()),
23+
new NullNode()),
24+
new NodeImpl("12",
25+
new NullNode(),
26+
new NodeImpl("122",
27+
new NullNode(),
28+
new NullNode())));
29+
30+
root.walk();
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Interface for binary tree node.
6+
*
7+
*/
8+
public interface Node {
9+
10+
String getName();
11+
int getTreeSize();
12+
Node getLeft();
13+
Node getRight();
14+
void walk();
15+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Implementation for binary tree's normal nodes.
6+
*
7+
*/
8+
public class NodeImpl implements Node {
9+
10+
private final String name;
11+
private final Node left;
12+
private final Node right;
13+
14+
public NodeImpl(String name, Node left, Node right) {
15+
this.name = name;
16+
this.left = left;
17+
this.right = right;
18+
}
19+
20+
@Override
21+
public int getTreeSize() {
22+
return 1 + left.getTreeSize() + right.getTreeSize();
23+
}
24+
25+
@Override
26+
public Node getLeft() {
27+
return left;
28+
}
29+
30+
@Override
31+
public Node getRight() {
32+
return right;
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return name;
38+
}
39+
40+
@Override
41+
public void walk() {
42+
System.out.println(name);
43+
if (left.getTreeSize() > 0) {
44+
left.walk();
45+
}
46+
if (right.getTreeSize() > 0) {
47+
right.walk();
48+
}
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Null Object implementation for binary tree node.
6+
*
7+
*/
8+
public class NullNode implements Node {
9+
10+
@Override
11+
public int getTreeSize() {
12+
return 0;
13+
}
14+
15+
@Override
16+
public Node getLeft() {
17+
return null;
18+
}
19+
20+
@Override
21+
public Node getRight() {
22+
return null;
23+
}
24+
25+
@Override
26+
public String getName() {
27+
return null;
28+
}
29+
30+
@Override
31+
public void walk() {
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar;
2+
3+
import org.junit.Test;
4+
5+
public class AppTest {
6+
7+
@Test
8+
public void test() {
9+
String[] args = {};
10+
App.main(args);
11+
}
12+
}

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
43
<modelVersion>4.0.0</modelVersion>
54

65
<groupId>com.iluwatar</groupId>
@@ -39,7 +38,8 @@
3938
<module>double-checked-locking</module>
4039
<module>servant</module>
4140
<module>service-locator</module>
42-
</modules>
41+
<module>null-object</module>
42+
</modules>
4343

4444
<dependencyManagement>
4545
<dependencies>
@@ -67,4 +67,4 @@
6767
</plugins>
6868
</build>
6969

70-
</project>
70+
</project>

0 commit comments

Comments
 (0)