Requirement: Develop a drawing program. The program has a menu bar with various tools that are used to manipulate a drawing. It also allows plugins to be developed and added to the Tool Bar.
We might model the problem like this:
What are the problems with this approach? a.
Not well encapsulated
Doesn’t follow the open-closed principle
Behavior is not intrinsic
The State pattern can be useful here. The State pattern is applicable when: a.
An object's behavior depends on its state, and it must change its behavior at run-time depending on that state. b.
Operations have large, multipart conditional statements that depend on the object's state. The
State pattern puts each branch of the conditional in a separate class.
The State pattern creates a separate State class for each state that encapsulates state specific data and behaviors. Thus, we take a situation as shown on the left and turn it into the design shown on the right.
Before State Pattern After State Pattern
Applying the state pattern to the drawing problem.
The State Pattern, “allows an object to alter its behavior when its internal state changes. The object will appear to change classes.” [GOF, p.305] a.
How does it alter its behavior when an internal state changes? An object is composed with an abstract state object. A change of state means that the object is composed with a different subclass which implements the behaviors differently. Thus, behaviors are altered. b.
How does the object appear to change classes? You ask the Context the same question at different times and get possible different answers. Thus, it is not the “same” Context it appears to have changed (classes).
Non-software example [http://sourcemaking.com/design_patterns/state]. The Vending machine is a good non-software example, although the class diagram shown in the link is a bit confusing. The idea is that when you select an item when you the result depends on the state (not enough money entered, enough money entered, out of stock, etc).
One important consideration is, how does the state change? There are two approaches
The context can handle state changes. State changes take place in the States themselves.
Advantage: States are not coupled to one another.
Disadvantage: Harder to add a State.
Advantage: Ease of adding States.
Disadvantage: Coupling between States.
Alarm Clock – See code on website
Sequence Diagram - Shows creating the alarm clock, setting the time, setting the alarm
Sequence Diagram (continued) - Shows alarm going off, pressing snooze
Sequence Diagram (continued) - Shows alarm going off after snooze and then turning alarm off.
Order Life Cycle
Notice that both patterns have the same general class diagram. So, how are they different? As usual, they are different in intent.
Strategies are typically changed from the outside, and sometimes from within the Context. States are frequently changed from within the states themselves.
A Strategy encapsulates an algorithm. A State encapsulates a set of behaviors appropriate to the
Each Strategy does the same thing, produces the same result, but goes about it different ways.
Strategies are interchangeable. States are not.
Strategies don’t know about other Strategies, they are uncoupled. States frequently do know about other States. They are coupled.
A Strategy is active, it encompasses the idea, in some sense, of processing the “state”. A sorting strategy sorts an array (state). A routing strategy uses the data (state) to produce a map. A State is
Describe, in general, the “normal” approach to implementing code based on a state diagram (p.386-
Describe the general situation where this “normal” approach is more acceptable.
Describe the shortcomings of the “normal” approach by examining the code on p.390-391.
What general types of things are contained in a State interface?
The state interface contains the actions.
Describe, in general, the State Pattern approach to implementing code based on a state diagram.
.DescrDescribe the approach of the state pattern.
Answer the Brain Power on page 405.
(Sketch on paper and bring to class) Look carefully at the code on page 403 and draw a class diagram.
Read the definition of the State pattern. What does it mean when it says, “The object will appear to
change its class.”
(Omit) Discuss the difference in intent between the Strategy and State patterns.
Where should state transitions take place and why?
Do clients ever interact with states?
Brainstorm several situations where the State pattern might be useful. Briefly discuss.
Wikipedia – Example with pen tool and selection tool in graphical application
Object Factory – An Account behaves differently depending on its balance. The difference in behavior is delegated to State objects called RedState, SilverState and GoldState.
Considers an Order and its different states: NewOrder, Registered, Granted, Shipped, 3.
Black Wasp – Media Player example with states: Standby, MP3 Playing, MP3 Paused, Radio
Informit – Garage door opener, with Observer pattern
Adobe – Video player
Unknown – Insertion in AVL tree, keeps track of state of balance in tree.
Sun Blog – States of a Patient in a hospital setting
State Oriented Programming – Embedded machines, hierarchical state machines.
State Oriented Programming – Various implementation issues including nested states and reflection, and command pattern.