==> Channel.java <== // Demo for simulation and state management "evap" // By RPFutrelle 4/3/01 for COM1204 /** * Class Channel in evap
* Shows state management in a simulation * * When message is inserted, channel goes into countdown * state and then nulls out ("evaporates") message. * Stepped by the Sim object. * * @author RP Futrelle * @version 1.0 */ public class Channel { public static void Main(String[] args) { // empty for } /** * Contents of the channel. */ String message; /** * State says counting down. */ boolean countingDown; /** * Countdown var, evaporate message when it == 0. */ int downCount; /** * Default constructor inits to empty channel. */ void Channel() { countingDown = false; message = null; } /** * Accepts string and places in message; starts countdown. * Important to note that no counting occurs until step() * is called -- the key to object autonomy in our simulation. */ void insertMessage(String str) { message = str; countingDown = true; downCount = 3; System.out.println("\nMessage \"" + str + "\" inserted."); } /** * As with all objects, responds to step(). */ void step() { if (countingDown){ if (downCount != 0) { // counting but not done downCount--; } else { // down to zero, so evaporate message and switch states countingDown = false; message = null; System.out.println("\nPooof! -- message evaporated"); } } else { // not counting, so do nothing -- idle } } // step() for Channel } // class Channel ==> Sim.java <== // Demo for simulation and state management "evap" // By RPFutrelle 4/3/01 for COM1204 /** * Class Sim in evap
* Shows state management in a simulation * * Creates the User and Channel objects * and steps them in a loop. * * @author RP Futrelle * @version 1.0 */ public class Sim { public static void main(String[] args) { Channel chan = new Channel(); User user = new User(); user.attachToChannel(chan); for(int i = 0; i < 25; i++){ user.step(); chan.step(); System.out.print(i + " "); } } } // class Sim /* Output of the above run: Message "hi" inserted. 0 1 2 Pooof! -- message evaporated 3 4 5 6 Message "hi" inserted. 7 8 9 Pooof! -- message evaporated 10 11 12 13 Message "hi" inserted. 14 15 16 Pooof! -- message evaporated 17 18 19 20 Message "hi" inserted. 21 22 23 Pooof! -- message evaporated */ ==> User.java <== // Demo for simulation and state management "evap" // By RPFutrelle 4/3/01 for COM1204 /** * Class User in evap
* Shows state management in a simulation * * User inserts message into channel and then goes into * wait state. Channel then goes into brief wait state * then "evaporates" message. Then user wakes up again * and drops in another message. All this is stepped * by the Sim object. * * @author RP Futrelle * @version 1.0 */ public class User { public static void Main(String[] args) { // empty for now } /** * The channel attached to this User. */ Channel myChannel; /** * State says counting down. */ boolean countingDown; /** * Countdown var, switch to insertion when it == 0. */ int downCount; /** * Default constructor inits to insertion state. */ void User() { countingDown = false; } /** * Attaches user to channel.
* (could have used constructor argument for this) */ void attachToChannel(Channel ch) { myChannel = ch; } /** * As with all objects, responds to step(). */ void step() { if (countingDown){ if (downCount != 0) { // counting but not done downCount--; } else { // down to zero, so switch states countingDown = false; } } else { // not counting, so insert message and start count myChannel.insertMessage("hi"); countingDown = true; downCount = 5; } } // step() for User } // class User