<< Chapter < Page Chapter >> Page >
Immutable lists are certainly very useful, but sometimes we naturally think of things as changing state. For instance, when we add an item to a list in real life, we don't throw away the old list; we mutate it to hold the new item. In this section we define the structure and behavior of a mutable list using a combination of the state dsign pattern and the visitor design pattern.

1. state pattern and dynamic reclassification

In programming, it is often necessary to have objects with which one can store data, retrieve data when needed, and remove data when no longer needed. Such objects are instances of what we call container structures.

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.

 

State transition diagram for container structures

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 directly support dynamic reclassification. 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 .

UML class diagram for the state design pattern

2. mutable linear recursive structure framework

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 .  Below is the UML class diagram of the LRStruct framework.  Because of the current limitation of our diagramming tool, we are using the Object[] input notation to represent the variable argument list Object... input Click here to download the codeClick here to download the javadoc documentation. . We will study the implementation code in the next lecture.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask