Rice University - Comp 212 - Intermediate Programming

Fall 2002

Lecture #12 - State Design Pattern and Application to Mutable Linear Recursive Structure


1. State Pattern and Dynamic Reclassification

A mutable container structure is a system that may change its state from empty to non-empty, and vice-versa.  For example, an empty container changes its state to non-empty after insertion of an object; and when the last element of a container is removed, its changes its state to empty.  Figure 1 below diagrams the state transition of a container structure.

 

For each distinct state, the algorithms to implement the methods differ.  For example, the algorithm for the retrieve method is trivial in the empty state -it simply returns null- while it is more complicated in the non-empty state.  The system thus behaves as if it changes classes dynamically.  This phenomenon is called “dynamic reclassification.”  The state pattern is a design solution for languages that do not support dynamic reclassification directly.  This pattern can be summarized as follow.

·         Define an abstract class for the states of the system.  This abstract state class should provide all the abstract methods for all the concrete subclasses.

·         Define a concrete subclass of the above abstract class for each state of the system. Each concrete state must implement its own concrete methods.

·         Represent the system by a class, called the context, containing an instance of a concrete state.  This instance represents the current state of the system.

·         Define methods for the system to return the current state and to change state.

·         Delegate all requests made to the system to the current state instance.  Since this instance can change dynamically, the system will behave as if it can change its class dynamically.

Below is the UML class diagram for the state design pattern.


2. Framework for Mutable Linear Recursive Structure

A mutable linear recursive structure (LRStruct) can be in the empty state or in a non-empty state. If it is empty, it contains no object. Otherwise, it contains an object called first, and a LRStruct object called rest. When we insert a data object into an empty LRStruct, it changes it state to non-empty.   When we remove the last element from an non-empty LRStruct, it changes its state to empty.  We model a LRStruct using the state pattern, and as in the case of the immutable list, we also apply the visitor pattern to obtain a framework.

The above public constructor and methods expose the structure of a LRStruct to the client and constitute the pure structural behavior of a LRStruct. They form a minimal and complete set of methods for manipulating a LRStruct. Using them, one can create an empty LRStruct, store data in it, and remove/retrieve data from it at will.  Below is a series of object diagrams showing snapshots of an instance of LRStruct at run-time.  At this point, do not concern yourself with how insertFront() and removeFront() are implemented.

 

3. LRStruct Visitor

Consider the problem of inserting an Integer object in order into a sorted list of Integers.  Let us contrast the insert in order algorithms between IList, the immutable list,  and LRStruct, the mutable list.

InsertInOrderWithFactory.java 
import listFW.*;

public class InsertInOrder implements IListAlgo {

  private IListFactory _fact;

  public InsertInOrder(IListFactory lf) {
      _fact = lf;
  }

  /**
  * Simply makes a new non-empty list with the given 
  * inp parameter as first.
  * @param host an empty IList.
  * @param inp an Integer to be inserted in order into host.
  * @return INEList.
  */
  public Object emptyCase(IEmptyList host, Object inp) {
      return _fact.makeNEList(inp, host);
  }

  /**
  * Based on the comparison between first and inp,
  * creates a new list or recur!
  * @param host a non-empty IList.
  * @param inp an Integer to be inserted in order into host.
  * @return INEList
  */
  public Object nonEmptyCase(INEList host, Object inp) {
      int n = ((Integer)inp).intValue();
      int f = ((Integer)host.getFirst()).intValue();
      return n < f ?
           _fact.makeNEList(inp, host):
           _fact.makeNEList(host.getFirst(),
                         (IList)host.getRest().execute(this, inp));
  }
}
InsertInOrderLRS.java 
import lrs.*;

public class InsertInOrderLRS implements IAlgo {

  public static final InsertInOrderLRS Singleton 
                                    = new InsertInOrderLRS();

  private InsertInOrderLRS() {
  }

  /**
  * Simply inserts the given inp parameter at the front.
  * @param host an empty LRStruct.
  * @param inp an Integer to be inserted in order into host.
  * @return LRStruct
  */
  public Object emptyCase(LRStruct host, Object inp) {
      return host.insertFront(inp);
  }

  /**
  * Based on the comparison between first and inp,
  * inserts at the front or recurs!
  * @param host a non-empty LRStruct.
  * @param inp an Integer to be inserted in order into host.
  * @return LRStruct
  */
  public Object nonEmptyCase(LRStruct host, Object inp) {
      int n = ((Integer)inp).intValue();
      int f = ((Integer)host.getFirst()).intValue();
      if (n < f) {
          return host.insertFront(inp);
      }
      else {
          return host.getRest().execute(this, inp);
      }
  }
}
Note that the insert in order algorithm for LRStruct need not creates any new list and thus needs no factory.

 

In class exercise: (time permitting)

Write an algorithm for LRStruct to remove the last element.  Throw an exception when the list is empty.

 

 


dxnguyen@cs.rice.edu
Copyright 2002, Dung X. Nguyen - All rights reserved.