Skip to content

Commit

Permalink
Null object as a singleton #50
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Apr 24, 2015
1 parent 19d653c commit 89e6692
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 99 deletions.
Binary file modified null-object/etc/test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 68 additions & 60 deletions null-object/etc/test.ucls
Original file line number Diff line number Diff line change
@@ -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>
12 changes: 6 additions & 6 deletions null-object/src/main/java/com/iluwatar/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public static void main( String[] args ) {
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();
}
Expand Down
77 changes: 44 additions & 33 deletions null-object/src/main/java/com/iluwatar/NullNode.java
Original file line number Diff line number Diff line change
@@ -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() {
}
}

0 comments on commit 89e6692

Please sign in to comment.