Added Null Object pattern.

This commit is contained in:
Ilkka Seppala 2015-03-04 22:59:42 +02:00
parent eb43f6efc3
commit 1cad280629
9 changed files with 225 additions and 4 deletions

BIN
null-object/etc/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

61
null-object/etc/test.ucls Normal file
View File

@ -0,0 +1,61 @@
<?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"/>
</class-diagram>

18
null-object/pom.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>null-object</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View 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();
}
}

View 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();
}

View 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();
}
}
}

View 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() {
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar;
import org.junit.Test;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
@ -39,7 +38,8 @@
<module>double-checked-locking</module> <module>double-checked-locking</module>
<module>servant</module> <module>servant</module>
<module>service-locator</module> <module>service-locator</module>
</modules> <module>null-object</module>
</modules>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>