AST Transformations
[ 220 ]
when: "PAUSED"
next = "LOADED"
event: "unload"
when: "LOADED"
next = "EMPTY"
Those old tape decks were quite stateful. You could only press pause if the tape
was running. You could only unload a tape if the tape was loaded but not running
or paused. There is a full state pattern version of the tape deck example in the
example code. Have a look; it has six classes and over a hundred lines of code. Yet
this complexity was all captured earlier in less than 25 lines of code. It also behaves
exactly as we expect. Here are just some of the allowed transitions in action. The
examples have a full test for them all:
given:
def tape = new TapeDeckState()
expect:
tape.state == 'EMPTY'
when:
tape.load()
then:
tape.state == 'LOADED'
when:
tape.start()
then:
tape.state == 'RUNNING'
when:
tape.start()
tape.pause()
then:
tape.state == 'PAUSED'
when:
tape.start()
when:
tape.pause()
tape.stop()
then:
tape.state == 'LOADED'
http://www.ebook3000.com