Null object as a singleton #50
This commit is contained in:
parent
19d653c13c
commit
89e6692ba8
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 15 KiB |
@ -1,61 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<interface id="1" language="java" name="com.iluwatar.Node" project="null-object"
|
||||
file="/null-object/src/main/java/com/iluwatar/Node.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="535" y="139"/>
|
||||
<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"
|
||||
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>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.NullNode" project="null-object"
|
||||
file="/null-object/src/main/java/com/iluwatar/NullNode.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="535" y="408"/>
|
||||
<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>
|
||||
</class>
|
||||
<association id="4">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="5" name="left"/>
|
||||
<multiplicity id="6" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="7">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="8" name="right"/>
|
||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="10">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<realization id="11">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<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"/>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<class id="1" language="java" name="com.iluwatar.NullNode" project="null-object"
|
||||
file="/null-object/src/main/java/com/iluwatar/NullNode.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<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>
|
||||
</class>
|
||||
<interface id="2" language="java" name="com.iluwatar.Node" project="null-object"
|
||||
file="/null-object/src/main/java/com/iluwatar/Node.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<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"
|
||||
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>
|
||||
</class>
|
||||
<association id="4">
|
||||
<end type="SOURCE" refId="1" navigable="false">
|
||||
<attribute id="5" name="instance"/>
|
||||
<multiplicity id="6" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="7">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</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 type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="11">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</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"
|
||||
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>
|
@ -18,14 +18,14 @@ public class App
|
||||
Node root = new NodeImpl("1",
|
||||
new NodeImpl("11",
|
||||
new NodeImpl("111",
|
||||
new NullNode(),
|
||||
new NullNode()),
|
||||
new NullNode()),
|
||||
NullNode.getInstance(),
|
||||
NullNode.getInstance()),
|
||||
NullNode.getInstance()),
|
||||
new NodeImpl("12",
|
||||
new NullNode(),
|
||||
NullNode.getInstance(),
|
||||
new NodeImpl("122",
|
||||
new NullNode(),
|
||||
new NullNode())));
|
||||
NullNode.getInstance(),
|
||||
NullNode.getInstance())));
|
||||
|
||||
root.walk();
|
||||
}
|
||||
|
@ -1,33 +1,44 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* Null Object implementation for binary tree node.
|
||||
*
|
||||
*/
|
||||
public class NullNode implements Node {
|
||||
|
||||
@Override
|
||||
public int getTreeSize() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getLeft() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getRight() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void walk() {
|
||||
}
|
||||
}
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* Null Object implementation for binary tree node.
|
||||
*
|
||||
* Implemented as Singleton, since all the NullNodes are the same.
|
||||
*
|
||||
*/
|
||||
public class NullNode implements Node {
|
||||
|
||||
private static NullNode instance = new NullNode();
|
||||
|
||||
private NullNode() {
|
||||
}
|
||||
|
||||
public static NullNode getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTreeSize() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getLeft() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node getRight() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void walk() {
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user