2015-05-31 11:55:18 +03:00
|
|
|
package com.iluwatar.nullobject;
|
2015-03-04 22:59:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Null Object pattern replaces null values with neutral objects.
|
|
|
|
* Many times this simplifies algorithms since no extra null checks
|
|
|
|
* are needed.
|
2015-08-20 22:42:49 +03:00
|
|
|
* <p>
|
2015-03-04 22:59:42 +02:00
|
|
|
* 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
|
|
|
|
{
|
2015-08-20 22:42:49 +03:00
|
|
|
/**
|
|
|
|
* Program entry point
|
|
|
|
* @param args command line args
|
|
|
|
*/
|
2015-03-04 22:59:42 +02:00
|
|
|
public static void main( String[] args ) {
|
|
|
|
|
|
|
|
Node root = new NodeImpl("1",
|
|
|
|
new NodeImpl("11",
|
|
|
|
new NodeImpl("111",
|
2015-04-24 22:01:08 +03:00
|
|
|
NullNode.getInstance(),
|
|
|
|
NullNode.getInstance()),
|
|
|
|
NullNode.getInstance()),
|
2015-03-04 22:59:42 +02:00
|
|
|
new NodeImpl("12",
|
2015-04-24 22:01:08 +03:00
|
|
|
NullNode.getInstance(),
|
2015-03-04 22:59:42 +02:00
|
|
|
new NodeImpl("122",
|
2015-04-24 22:01:08 +03:00
|
|
|
NullNode.getInstance(),
|
|
|
|
NullNode.getInstance())));
|
2015-03-04 22:59:42 +02:00
|
|
|
|
|
|
|
root.walk();
|
|
|
|
}
|
|
|
|
}
|