Source: "Agile Software Development: Principles, Patterns, and Practices" by Robert C. Martin. Available at www.objectmentor.com/PPP.

import java.util.Date;

public interface Employee
{
  public boolean isTimeToPay(Date payDate);

  public void pay();

  public static final Employee NULL = new Employee()
  {
    public boolean isTimeToPay(Date payDate)
    {
      return false;
    }

    public void pay()
    {
    }
  };
}


public class DB
{
  public static Employee getEmployee(String name)
  {
    return Employee.NULL;
  }
}



import junit.framework.TestCase;
import junit.swingui.TestRunner;

import java.util.Date;

public class TestEmployee extends TestCase
{
  public static void main(String[] args)
  {
    TestRunner.main(new String[]{"TestEmployee"});
  }

  public TestEmployee(String name)
  {
    super(name);
  }

  public void setUp() throws Exception
  {
  }

  public void tearDown() throws Exception
  {
  }

  public void testNull() throws Exception
  {
    Employee e = DB.getEmployee("Bob");
    if (e.isTimeToPay(new Date()))
      fail();
    assertEquals(Employee.NULL, e);
  }
}


This is an example from CSci 3601 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.