Chaper 1 Questions

advertisement

Chaper 10 Notes – State Pattern

Motivating Example

1.

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.

2.

We might model the problem like this:

3.

What are the problems with this approach? a.

Not object-oriented b.

Not well encapsulated c.

Doesn’t follow the open-closed principle d.

Not flexible e.

Behavior is not intrinsic

1

4.

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.

5.

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

6.

Applying the state pattern to the drawing problem.

Context

2

1.

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).

2.

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).

3.

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.  Advantage: Ease of adding States.

 Disadvantage: Harder to add a State.  Disadvantage: Coupling between States.

3

Examples

1.

Alarm Clock – See code on website

State Diagram

Class Diagram

4

Sequence Diagram - Shows creating the alarm clock, setting the time, setting the alarm

Sequence Diagram (continued) - Shows alarm going off, pressing snooze

5

Sequence Diagram (continued) - Shows alarm going off after snooze and then turning alarm off.

2.

Game

http://translate.google.com/translate?hl=en&ie=UTF-

8&sl=de&tl=en&u=http://code.google.com/p/btuswphalma/wiki/GuiEntwurf&prev=_t&tw u=1

3.

Order Life Cycle

http://ooxs-be.goracer.nl/EN/java/design-patterns/State%20Pattern.html

6

Difference between State & Strategy

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

State.

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

passive.

Text Discussion

1.

Describe, in general, the “normal” approach to implementing code based on a state diagram (p.386-

396).

2.

Describe the general situation where this “normal” approach is more acceptable.

3.

Describe the shortcomings of the “normal” approach by examining the code on p.390-391.

4.

What general types of things are contained in a State interface?

The state interface contains the actions.

5.

Describe, in general, the State Pattern approach to implementing code based on a state diagram.

.DescrDescribe the approach of the state pattern.

6.

Answer the Brain Power on page 405.

7.

(Sketch on paper and bring to class) Look carefully at the code on page 403 and draw a class diagram.

8.

Read the definition of the State pattern. What does it mean when it says, “The object will appear to change its class.”

9.

(Omit) Discuss the difference in intent between the Strategy and State patterns.

10.

Where should state transitions take place and why?

11.

Do clients ever interact with states?

12.

Brainstorm several situations where the State pattern might be useful. Briefly discuss.

7

References

1.

Wikipedia – Example with pen tool and selection tool in graphical application

2.

Object Factory – An Account behaves differently depending on its balance. The difference in behavior is delegated to State objects called RedState, SilverState and GoldState.

3.

Code Project – Considers an Order and its different states: NewOrder, Registered, Granted, Shipped,

Invoiced, Cancelled.

4.

Black Wasp – Media Player example with states: Standby, MP3 Playing, MP3 Paused, Radio

5.

Informit – Garage door opener, with Observer pattern

6.

Adobe – Video player

7.

Unknown – Insertion in AVL tree, keeps track of state of balance in tree.

8.

Sun Blog – States of a Patient in a hospital setting

9.

State Oriented Programming – Embedded machines, hierarchical state machines.

10.

State Oriented Programming – Various implementation issues including nested states and reflection, and command pattern.

8

Download