Laboratory: Chain of Responsibility Pattern
CSC 207 Algorithms and Object Oriented Design Spring 2011

Summary: In this lab, you will implement a small motivating example of the chain of responsibility pattern.

Background

As described in the reading, the chain of responsibility pattern features a linked list of handlers that each determine whether they are (or should be) responsible for processing a request.

Let's consider an environment where each task has a priority.

Task.java
/** A class representing a task to be completed. Each task has
 *  priority level and a meaningful description. 
 * 
 * @author Jerod Weinman
 */
public class Task
{
  /** Create a task with the specified description and priority */
  public Task(String description, int priority)
  {
    this.description = description;
    this.priority = priority;
  }

  /** Return the priority of this task */
  public int getPriority() { return priority; }

  /** Return the description of this task */
  public String getDescription() { return description; }

  private String description; /** Description of this task */
  private int priority; /** Priority of this task */
}
Of course, tasks a pretty useless without some folks who are responsible for processing them. In this lab, you'll be creating a chain of workers who are only authorized to handle tasks of a particular priority.

Exercises

Exercise 1

Create an class called Worker that has the following members:

Exercise 2

Add a method to your Worker class called process whose sole parameter is a Task and returns nothing.

If the task priority does not exceed the worker's threshold, the worker should handle the task by printing its name and the description of the task. Otherwise, it should pass the task to its superior.

Exercise 3

In a separate driver class, create workers in a chain of responsibility with the following names and priorities:
NamePriority
President6
Vice-President5
Director4
Manager3
Staffer2
Receptionist1

Exercise 3

Pass several tasks of various priorities to the receptionist and confirm that they are handled by the appropriate workers.

Epilogue

Note that in a more realistic setting, you would likely create an abstract worker class (or interface) and derive several concrete subclasses rather than using strings to differentiate the capabilitis.

For those with extra time

The reading described how handling requests can be decoupled from the chain. Create a TaskHandler interface with a handle method that accepts a Task and returns nothing.

Derive a few concrete handlers that each store a string representing the means of handling a task, such as "making a phone call" or "sitting on hands".

Change your Worker class so that it also HAS-A task handler. When the worker processes the task, it should then pass it along to its handler.

Update your driver. It might produce output that is something like this:

Receptionist handles flower delivery by making a phone call
Mortgage bank CFO handles financial meltdown by sitting on hands
Created: Jerod Weinman, 20 January 2011