diff --git a/null-object/etc/test.png b/null-object/etc/test.png new file mode 100644 index 000000000..a29c0a617 Binary files /dev/null and b/null-object/etc/test.png differ diff --git a/null-object/etc/test.ucls b/null-object/etc/test.ucls new file mode 100644 index 000000000..569663338 --- /dev/null +++ b/null-object/etc/test.ucls @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/null-object/pom.xml b/null-object/pom.xml new file mode 100644 index 000000000..276631e6a --- /dev/null +++ b/null-object/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + null-object + + + junit + junit + test + + + diff --git a/null-object/src/main/java/com/iluwatar/App.java b/null-object/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000..4bba5af29 --- /dev/null +++ b/null-object/src/main/java/com/iluwatar/App.java @@ -0,0 +1,32 @@ +package com.iluwatar; + +/** + * + * Null Object pattern replaces null values with neutral objects. + * Many times this simplifies algorithms since no extra null checks + * are needed. + * + * In this example we build a binary tree where the nodes are either + * normal or Null Objects. No null values are used in the tree making + * the traversal easy. + * + */ +public class App +{ + public static void main( String[] args ) { + + Node root = new NodeImpl("1", + new NodeImpl("11", + new NodeImpl("111", + new NullNode(), + new NullNode()), + new NullNode()), + new NodeImpl("12", + new NullNode(), + new NodeImpl("122", + new NullNode(), + new NullNode()))); + + root.walk(); + } +} diff --git a/null-object/src/main/java/com/iluwatar/Node.java b/null-object/src/main/java/com/iluwatar/Node.java new file mode 100644 index 000000000..1bb94927f --- /dev/null +++ b/null-object/src/main/java/com/iluwatar/Node.java @@ -0,0 +1,15 @@ +package com.iluwatar; + +/** + * + * Interface for binary tree node. + * + */ +public interface Node { + + String getName(); + int getTreeSize(); + Node getLeft(); + Node getRight(); + void walk(); +} diff --git a/null-object/src/main/java/com/iluwatar/NodeImpl.java b/null-object/src/main/java/com/iluwatar/NodeImpl.java new file mode 100644 index 000000000..dbd5a7eca --- /dev/null +++ b/null-object/src/main/java/com/iluwatar/NodeImpl.java @@ -0,0 +1,50 @@ +package com.iluwatar; + +/** + * + * Implementation for binary tree's normal nodes. + * + */ +public class NodeImpl implements Node { + + private final String name; + private final Node left; + private final Node right; + + public NodeImpl(String name, Node left, Node right) { + this.name = name; + this.left = left; + this.right = right; + } + + @Override + public int getTreeSize() { + return 1 + left.getTreeSize() + right.getTreeSize(); + } + + @Override + public Node getLeft() { + return left; + } + + @Override + public Node getRight() { + return right; + } + + @Override + public String getName() { + return name; + } + + @Override + public void walk() { + System.out.println(name); + if (left.getTreeSize() > 0) { + left.walk(); + } + if (right.getTreeSize() > 0) { + right.walk(); + } + } +} diff --git a/null-object/src/main/java/com/iluwatar/NullNode.java b/null-object/src/main/java/com/iluwatar/NullNode.java new file mode 100644 index 000000000..5f0b440be --- /dev/null +++ b/null-object/src/main/java/com/iluwatar/NullNode.java @@ -0,0 +1,33 @@ +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() { + } +} diff --git a/null-object/src/test/java/com/iluwatar/AppTest.java b/null-object/src/test/java/com/iluwatar/AppTest.java new file mode 100644 index 000000000..6db5ad214 --- /dev/null +++ b/null-object/src/test/java/com/iluwatar/AppTest.java @@ -0,0 +1,12 @@ +package com.iluwatar; + +import org.junit.Test; + +public class AppTest { + + @Test + public void test() { + String[] args = {}; + App.main(args); + } +} diff --git a/pom.xml b/pom.xml index da149fdf8..92d10e951 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,5 @@ - + 4.0.0 com.iluwatar @@ -39,7 +38,8 @@ double-checked-locking servant service-locator - + null-object + @@ -67,4 +67,4 @@ - + \ No newline at end of file