Null object as a singleton #50

This commit is contained in:
Ilkka Seppala committed 2015-04-24 22:01:08 +03:00
1 parent 19d653c13c
commit 89e6692ba8
4 files changed
+52 -33

No files matched your search

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 15 KiB

+35 -27
View File
@@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true" <class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true"> realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<interface id="1" language="java" name="com.iluwatar.Node" project="null-object" <class id="1" language="java" name="com.iluwatar.NullNode" project="null-object"
file="/null-object/src/main/java/com/iluwatar/Node.java" binary="false" corner="BOTTOM_RIGHT"> file="/null-object/src/main/java/com/iluwatar/NullNode.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="535" y="139"/> <position height="-1" width="-1" x="123" y="427"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="2" language="java" name="com.iluwatar.NodeImpl" project="null-object"
file="/null-object/src/main/java/com/iluwatar/NodeImpl.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="857" y="348"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true"> sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/> <attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/> <operations public="true" package="true" protected="true" private="true" static="true"/>
</display> </display>
</class> </class>
<class id="3" language="java" name="com.iluwatar.NullNode" project="null-object" <interface id="2" language="java" name="com.iluwatar.Node" project="null-object"
file="/null-object/src/main/java/com/iluwatar/NullNode.java" binary="false" corner="BOTTOM_RIGHT"> file="/null-object/src/main/java/com/iluwatar/Node.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="535" y="408"/> <position height="-1" width="-1" x="123" y="164"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="3" language="java" name="com.iluwatar.NodeImpl" project="null-object"
file="/null-object/src/main/java/com/iluwatar/NodeImpl.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="399" y="164"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true"> sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/> <attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -29,29 +29,37 @@
</display> </display>
</class> </class>
<association id="4"> <association id="4">
<end type="SOURCE" refId="2" navigable="false"> <end type="SOURCE" refId="1" navigable="false">
<attribute id="5" name="left"/> <attribute id="5" name="instance"/>
<multiplicity id="6" minimum="0" maximum="1"/> <multiplicity id="6" minimum="0" maximum="1"/>
</end> </end>
<end type="TARGET" refId="1" navigable="true"/> <end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/> <display labels="true" multiplicity="true"/>
</association> </association>
<association id="7"> <realization id="7">
<end type="SOURCE" refId="2" navigable="false"> <end type="SOURCE" refId="1"/>
<attribute id="8" name="right"/> <end type="TARGET" refId="2"/>
<multiplicity id="9" minimum="0" maximum="1"/> </realization>
<association id="8">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="9" name="right"/>
<multiplicity id="10" minimum="0" maximum="1"/>
</end> </end>
<end type="TARGET" refId="1" navigable="true"/> <end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/> <display labels="true" multiplicity="true"/>
</association> </association>
<realization id="10">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</realization>
<realization id="11"> <realization id="11">
<end type="SOURCE" refId="3"/> <end type="SOURCE" refId="3"/>
<end type="TARGET" refId="1"/> <end type="TARGET" refId="2"/>
</realization> </realization>
<association id="12">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="13" name="left"/>
<multiplicity id="14" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true"> sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/> <attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -18,14 +18,14 @@ public class App
Node root = new NodeImpl("1", Node root = new NodeImpl("1",
new NodeImpl("11", new NodeImpl("11",
new NodeImpl("111", new NodeImpl("111",
new NullNode(), NullNode.getInstance(),
new NullNode()), NullNode.getInstance()),
new NullNode()), NullNode.getInstance()),
new NodeImpl("12", new NodeImpl("12",
new NullNode(), NullNode.getInstance(),
new NodeImpl("122", new NodeImpl("122",
new NullNode(), NullNode.getInstance(),
new NullNode()))); NullNode.getInstance())));
root.walk(); root.walk();
} }
@@ -4,9 +4,20 @@ package com.iluwatar;
* *
* Null Object implementation for binary tree node. * Null Object implementation for binary tree node.
* *
* Implemented as Singleton, since all the NullNodes are the same.
*
*/ */
public class NullNode implements Node { public class NullNode implements Node {
private static NullNode instance = new NullNode();
private NullNode() {
}
public static NullNode getInstance() {
return instance;
}
@Override @Override
public int getTreeSize() { public int getTreeSize() {
return 0; return 0;