X Tutup
Skip to content

Commit 89e6692

Browse files
committed
Null object as a singleton iluwatar#50
1 parent 19d653c commit 89e6692

File tree

4 files changed

+118
-99
lines changed

4 files changed

+118
-99
lines changed

null-object/etc/test.png

-1.78 KB
Loading

null-object/etc/test.ucls

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,69 @@
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"/>
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+
<class id="1" language="java" name="com.iluwatar.NullNode" project="null-object"
5+
file="/null-object/src/main/java/com/iluwatar/NullNode.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="123" y="427"/>
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+
</class>
13+
<interface id="2" language="java" name="com.iluwatar.Node" project="null-object"
14+
file="/null-object/src/main/java/com/iluwatar/Node.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="123" y="164"/>
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+
</interface>
22+
<class id="3" language="java" name="com.iluwatar.NodeImpl" project="null-object"
23+
file="/null-object/src/main/java/com/iluwatar/NodeImpl.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="399" y="164"/>
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="1" navigable="false">
33+
<attribute id="5" name="instance"/>
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+
<realization id="7">
40+
<end type="SOURCE" refId="1"/>
41+
<end type="TARGET" refId="2"/>
42+
</realization>
43+
<association id="8">
44+
<end type="SOURCE" refId="3" navigable="false">
45+
<attribute id="9" name="right"/>
46+
<multiplicity id="10" minimum="0" maximum="1"/>
47+
</end>
48+
<end type="TARGET" refId="2" navigable="true"/>
49+
<display labels="true" multiplicity="true"/>
50+
</association>
51+
<realization id="11">
52+
<end type="SOURCE" refId="3"/>
53+
<end type="TARGET" refId="2"/>
54+
</realization>
55+
<association id="12">
56+
<end type="SOURCE" refId="3" navigable="false">
57+
<attribute id="13" name="left"/>
58+
<multiplicity id="14" minimum="0" maximum="1"/>
59+
</end>
60+
<end type="TARGET" refId="2" navigable="true"/>
61+
<display labels="true" multiplicity="true"/>
62+
</association>
63+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
64+
sort-features="false" accessors="true" visibility="true">
65+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
66+
<operations public="true" package="true" protected="true" private="true" static="true"/>
67+
</classifier-display>
68+
<association-display labels="true" multiplicity="true"/>
6169
</class-diagram>

null-object/src/main/java/com/iluwatar/App.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public static void main( String[] args ) {
1818
Node root = new NodeImpl("1",
1919
new NodeImpl("11",
2020
new NodeImpl("111",
21-
new NullNode(),
22-
new NullNode()),
23-
new NullNode()),
21+
NullNode.getInstance(),
22+
NullNode.getInstance()),
23+
NullNode.getInstance()),
2424
new NodeImpl("12",
25-
new NullNode(),
25+
NullNode.getInstance(),
2626
new NodeImpl("122",
27-
new NullNode(),
28-
new NullNode())));
27+
NullNode.getInstance(),
28+
NullNode.getInstance())));
2929

3030
root.walk();
3131
}
Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
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-
}
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Null Object implementation for binary tree node.
6+
*
7+
* Implemented as Singleton, since all the NullNodes are the same.
8+
*
9+
*/
10+
public class NullNode implements Node {
11+
12+
private static NullNode instance = new NullNode();
13+
14+
private NullNode() {
15+
}
16+
17+
public static NullNode getInstance() {
18+
return instance;
19+
}
20+
21+
@Override
22+
public int getTreeSize() {
23+
return 0;
24+
}
25+
26+
@Override
27+
public Node getLeft() {
28+
return null;
29+
}
30+
31+
@Override
32+
public Node getRight() {
33+
return null;
34+
}
35+
36+
@Override
37+
public String getName() {
38+
return null;
39+
}
40+
41+
@Override
42+
public void walk() {
43+
}
44+
}

0 commit comments

Comments
 (0)
X Tutup