Null object as a singleton #50

This commit is contained in:
Ilkka Seppala 2015-04-24 22:01:08 +03:00
parent 19d653c13c
commit 89e6692ba8
4 changed files with 118 additions and 99 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -1,61 +1,69 @@
<?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" <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>
</interface> </class>
<class id="2" language="java" name="com.iluwatar.NodeImpl" project="null-object" <interface id="2" language="java" name="com.iluwatar.Node" project="null-object"
file="/null-object/src/main/java/com/iluwatar/NodeImpl.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="857" y="348"/> <position height="-1" width="-1" x="123" 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"/>
<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> </interface>
<class id="3" language="java" name="com.iluwatar.NullNode" project="null-object" <class id="3" language="java" name="com.iluwatar.NodeImpl" 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/NodeImpl.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="535" y="408"/> <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"/>
<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>
<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>
</end> <association id="8">
<end type="TARGET" refId="1" navigable="true"/> <end type="SOURCE" refId="3" navigable="false">
<display labels="true" multiplicity="true"/> <attribute id="9" name="right"/>
</association> <multiplicity id="10" minimum="0" maximum="1"/>
<realization id="10"> </end>
<end type="SOURCE" refId="2"/> <end type="TARGET" refId="2" navigable="true"/>
<end type="TARGET" refId="1"/> <display labels="true" multiplicity="true"/>
</realization> </association>
<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>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" <association id="12">
sort-features="false" accessors="true" visibility="true"> <end type="SOURCE" refId="3" navigable="false">
<attributes public="true" package="true" protected="true" private="true" static="true"/> <attribute id="13" name="left"/>
<operations public="true" package="true" protected="true" private="true" static="true"/> <multiplicity id="14" minimum="0" maximum="1"/>
</classifier-display> </end>
<association-display labels="true" multiplicity="true"/> <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"
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"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram> </class-diagram>

View File

@ -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();
} }

View File

@ -1,33 +1,44 @@
package com.iluwatar; 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 { *
*/
@Override public class NullNode implements Node {
public int getTreeSize() {
return 0; private static NullNode instance = new NullNode();
}
private NullNode() {
@Override }
public Node getLeft() {
return null; public static NullNode getInstance() {
} return instance;
}
@Override
public Node getRight() { @Override
return null; public int getTreeSize() {
} return 0;
}
@Override
public String getName() { @Override
return null; public Node getLeft() {
} return null;
}
@Override
public void walk() { @Override
} public Node getRight() {
} return null;
}
@Override
public String getName() {
return null;
}
@Override
public void walk() {
}
}