|
Comp201: Principles of Object-Oriented Programming I
|
Examples of changing state:
The cat and the kitten are the same animal, but they don't act identically. A car can be driven but a wreck cannot--yet they are the same entity fundamentally. Your friend is the same human being, no matter what their mood. Why shouldn't a list be the same list and a fractal be the same fractal?
When something changes state, it is the same object, but yet it behaves differently. This phenomenon of having an objects change its behavior as if it were suddenly belonging to a whole different class of objects is called "dynamic reclassification".
So far we've been using immutable data, and to create a non-empty list from an empty one, required that we make a whole brand-new list. With our use assignment ("=") previously, we've changed the value of a variable, but never the behavior the object it references.
Consider this notion: We want to change the type of the object but we want to encapsulate that change so that the outside world doesn't see the type change, only the behavior change.
Let's work with an example:
Remember the old arcade game, "Frogger"? That's the one where a traffic-challenged amphibian attempts to hop across multiple lanes of speeding cars and trucks, hopefully without being converted into the road-kill-du-jour.
(Here's an on-line version: http://www.gamesgnome.com/arcade/frogger/)
Well, let's look at what a frog is here:
A live frog
On the other hand, a dead frog
Using our trusty separation of variant and invariant, we can see that the position of a frog is an invariant but all the other behaviors are variants. Thus we want to separate out these variants into their own subclasses of an invariant abstract class. We then use composition to model the frog having an abstract state, which could be either alive or dead:
Click on any class below to see its source code. Download
the code here.
Click here to see the full documentation
of the above code.
The variant behaviors are represented by the abstract AFrogState, with the DeadState and LiveState overriding its abstract behaviors.
(Note: The IFrog interface is there simply to allow different formulations of the frog to exist. See the Question below.)
For those variant behaviors, all the main Frog does is to delegate (pass the call on to and return the result of) to the _state that it has. If the _state is a LiveState, then the Frog will act as if were alive because the LiveState only contains live-like behaviors. On the other hand, if the state is a DeadState, then the delegation to the _state will produce dead-like behavior. The LiveState's getHit behavior will cause the Frog's _state to change from referencing a LiveState instance to referencing a DeadState instance.
No conditionals are needed!!
The Frog behaves the way it does because of what it is at that moment, not because of what it can figure out about itself then.
This is an example of the State Design Pattern. More information on this design pattern can be found at: http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/StatePat.htm
From the outside, nothing about the internal implementation of Frog can be seen. All one can see is its public behavior. The implementations of the state design pattern are completely encapsulated (within the frog package, in this formulation).. For instance, if one is moving a live Frog, it will dutifully move as directed, but if in the middle somewhere, the Frog is hit, then it will immediately stop moving, no matter how much it is asked to do so. If one is checking its color, the live Frog is a healthy green but right after its accident, it will report that it is deathly red.
Notice how the Frog changes its behavior and always behaves correctly for its situation, with no conditional statements whatsoever.
A very nice technique, when it is possible, is to implement the State pattern using anonymous inner classes. Can you write an IFrog implementation that encapsulates the states using nested class(es)and anonymous inner class(es)?
I highly recommend that you work this question out. You know what I mean? Hint, hint, nudge, nudge...
Looking way back to the beginning of the semester, we now have to ask, "Can we use this technology to create a mutable list? How will this affect the visitors and their execute function?" Hmmmm.....
Last Revised Thursday, 03-Jun-2010 09:50:32 CDT
©2008 Stephen Wong and Dung Nguyen