Added Null Object pattern.
This commit is contained in:
32
null-object/src/main/java/com/iluwatar/App.java
Normal file
32
null-object/src/main/java/com/iluwatar/App.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
15
null-object/src/main/java/com/iluwatar/Node.java
Normal file
15
null-object/src/main/java/com/iluwatar/Node.java
Normal file
@ -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();
|
||||
}
|
50
null-object/src/main/java/com/iluwatar/NodeImpl.java
Normal file
50
null-object/src/main/java/com/iluwatar/NodeImpl.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
33
null-object/src/main/java/com/iluwatar/NullNode.java
Normal file
33
null-object/src/main/java/com/iluwatar/NullNode.java
Normal file
@ -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() {
|
||||
}
|
||||
}
|
12
null-object/src/test/java/com/iluwatar/AppTest.java
Normal file
12
null-object/src/test/java/com/iluwatar/AppTest.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user