16
Andy Bulka Technical Director Austhink Software www.austhink.com

State Pattern in Flex

Embed Size (px)

DESCRIPTION

Presentation by Andy Bulka on the State design pattern with examples in Flex presented to the Melbourne Patterns Group in September 2008

Citation preview

Page 1: State Pattern in Flex

Andy BulkaTechnical DirectorAusthink Softwarewww.austhink.com

Page 2: State Pattern in Flex

State Alter an object's behavior when its state

changes. Put the “state” in a class and delegate to it.

IntentAllow an object to alter its behavior when its

internal state changes. The object will appear to change its class.

Page 3: State Pattern in Flex

State pattern - UMLClient code talks to the “context.Request()”

which behaves differently

Page 4: State Pattern in Flex

So what if you want to add the new "Orange" state? You usually have to modify code in a number of places.

Page 5: State Pattern in Flex
Page 6: State Pattern in Flex

State instances as SingletonsYou often want to have singleton states

rather than recreating each state time and time again, and having to copy across any state attributes each time. Of course by keeping most of the attributes in the context, there is not much data in state to copy between state instance when the state switches.

Page 7: State Pattern in Flex

What States instances can accessOne main issue in state pattern is how do

state instances access attributes in the context. Simplest solution is to have a pointer back from each state object back to the context. Another solution is to completely decouple the state from the context and simply pass the state instance what it needs e.g. as parameters to state methods (as defined in the the state interface)

Page 8: State Pattern in Flex

Who changes the stateSometimes client code drives the changing of the

stateSometimes client code holds the state instances

too, though I prefer to map these to enums and look them up in a dictionary (see singleton issue above)

Often the states themselves or the context (e.g. car) will take on this responsibility. If the states are given the power to set the next state – they will need need access to the context (via a backpointer) so that they can call a method called e.g. SetState().

Page 9: State Pattern in Flex

Flex 3 example

Page 10: State Pattern in Flex

Flex 3 classesImplement dumb

graphic in either flash or mxml

Deep state pattern switching logic is in pure actionscript code

Hook up the gui to the pattern using various techniques

Page 11: State Pattern in Flex

Flash – dumb graphicGraphics

reified as symbols

Symbols given names

Symbols marked as inheriteing from Flex’s mx.flash.UIMovieClip

Page 12: State Pattern in Flex

UML 1Simple version – flash component subclasses

“context”

Page 13: State Pattern in Flex

UML vers2

Page 14: State Pattern in Flex

Re the behaviour - display()Three versions of

implementing the graphic part is shown.

pure mxml (code embedded inside the mxml)

mxml + actionscript class

flash component (dumb)

Page 15: State Pattern in Flex

Flex 3 MXML graphics

Page 16: State Pattern in Flex

End.

AdvertDesign Pattern Courses - for your companyElevate the effectiveness of your entire programming

team - Andy can deliver a design patterns course to you programming team on your company's premises.  Check out the details here.

http://www.atug.com/andypatterns/training_courses.htm