[an error occurred while processing this directive]

Part I. Objects
People-Oriented API Design
Guideline 3. Understand the kinship between objects and state machines
by Bill Venners

<<  Page 6 of 20  >>

A Stamp Dispenser State Machine

Occasionally, you can describe the behavior of objects in state machine terms. For example, imagine an object that models the behavior of an extremely simple stamp dispenser described by these requirements:

Write control software for an automated stamp dispenser. The stamp dispenser accepts only nickels (5 cents) and dimes (10 cents) and dispenses only 20-cent stamps. The stamp dispenser's LED display indicates to the user the total amount of money inserted so far. As soon as 20 or more cents is inserted, a 20-cent stamp automatically dispenses along with any change. The only amounts the display shows, therefore, are 0, 5, 10, and 15 cents. If a dime and a nickel have been inserted, the display indicates 15 cents. If the user then inserts another dime, the stamp dispenser dispenses a 20-cent stamp, returns a nickel, and changes the display to show 0 cents. In addition to a coin slot, an opening for dispensing stamps and change, and an LED display, the stamp dispenser also has a coin return lever. When the user presses coin return, the stamp dispenser returns the amount of money indicated on the display, and changes the display to show 0 cents.

You could also describe the behavior of this simple stamp dispenser in terms of a state machine that has:

A stamp dispenser's current state indicates how much money has been inserted. If no money has been inserted, the stamp dispenser is in HAS_0 state. If a nickel has been inserted, the stamp dispenser is in HAS_5 state, and so on. No HAS_20 state appears in the list, because as soon as 20 cents is inserted, a stamp is automatically issued and any change is returned.

The three messages represent the actions a stamp dispenser user can take: inserting a nickel (add5), inserting a dime (add10), or pressing the coin return lever (returnCoins). The four actions the stamp dispenser can take are return a nickel (ret5), return a dime (ret10), return 15 cents (ret15), or dispense a 20 cent stamp (dispenseStamp).

Figure 3-1 shows the state-transition diagram that defines the actions taken and next state that results from a stamp dispenser receiving each message in each state.

Figure 3-1. The stamp dispenser state-transition diagram

In Figure 3-1, each of the four states, HAS_0, HAS_5, HAS_10, and HAS_15, is represented by a circle. The circle labeled start with an arrow pointing to the HAS_0 state indicates the state machine's initial state is HAS_0. State transitions are shown by arrows between states. Each arrow is labeled with the message that causes the transition and, if any actions are required to accompany the state transistion, a forward slash plus the required actions. For example, an arrow from HAS_10 to HAS_0 is labeled add10/dispenseStamp. This arrow indicates that if an add10 message is received while the state machine is in the HAS_10 state, the machine should change to the HAS_0 state and perform the dispenseStamp action.

<<  Page 6 of 20  >>

People-Oriented API Design | Contents | Book List | Print | Email | First Page | Previous | Next

Sponsored Links
Download Artima SuiteRunner Now - It's FREE!

Last Updated: Sunday, May 11, 2003
Copyright © 1996-2003 Artima Software, Inc. All Rights Reserved.
URL: is created by Bill Venners