State Pattern – Lecture Slides | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Memon; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-zus
koofers-user-zus 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
State Pattern
Apr. 19, 2007
2
An Example
Let’s create a drawing program that
will have toolbar buttons for Pick,
Rectangle, Fill, Circle and Clear.
Each one of the tool buttons does
something rather different whe n it
is selected and you click or drag
your mouse across the screen. For
example,
If the Pick button is selected,
clicking inside a drawing element
should cause it to be highlighted or
appear with “handles.”
If the mouse is dragged and a
drawing element is already
selected, the element should move
on the screen.
If the Rect button is selected,
clicking on the screen should cause
a new rectangle drawing element
to be created.
3
An Example
If the Fill button is selected and a
drawing element is already
selected, that element should be
filled with the current colo r.
If no drawing is selected, then
clicking inside a drawing should
fill it with the current colo r.
If the Circle button is selected,
clicking on the screen should cause
a new circle drawing element to be
created.
If the Clear button is select ed, all
the drawing elements are removed.
•Thus, the state of the graphical
editor affects the behavior the
program should exhibit.
4
A Possible Solution
We might design our program with a Mediator
managing the actions of 5 command buttons.
pf3
pf4
pf5

Partial preview of the text

Download State Pattern – Lecture Slides | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Apr. 19, 2007State PatternSpring 2007

An Example

  • example,your mouse across the screen. Foris selected and you click or dragsomething rather different when it•^ Each one of the tool buttons doesRectangle, Fill, Circle and Clear.will have toolbar buttons for Pick,•^ Let’s create a drawing program that to be created.a new rectangle drawing elementclicking on the screen should cause–^ If the Rect button is selected,on the screen.selected, the element should movedrawing element is already–^ If the mouse is dragged and aappear with “handles.”should cause it to be highlighted orclicking inside a drawing element^ If the Pick button is selected,

3

An Example

  • (^) Thus, thethe drawing elements are removed.– (^) If the Clear button is selected, allcreated.a new circle drawing element to beclicking on the screen should cause– (^) If the Circle button is selected,fill it with the current color.clicking inside a drawing should– (^) If no drawing is selected, thenfilled with the current color.selected, that element should bedrawing element is already– (^) If the Fill button is selected and a (^) state of the graphical

program should exhibit.editor affects the behavior the

A Possible Solution

managing the actions of 5 command buttons.•^ We might design our program with a Mediator

5

However…

complicated as well as leading to a set ofactivity inside the Mediator can make it unduly•^ Keeping the state of the buttons and the desired mousebetween various controls, such as the buttons.main purpose of a Mediator is to coordinate activitiesstate of the program on the Mediator, and we know that the•^ This initial design puts the entire burden of maintaining the

(^) if (^) or (^) switch (^) tests

  • (^) This makes the program very hard to read and maintain.mouseUp, mouseDrag, rightClick and so forth.repeated for each action the Mediator interprets, such as• (^) Further, this set of conditional statements might have to bewhich make the program difficult to read and maintain.

New Design Strategy

  • Four of them use the mouse click event to causethe actions we should explore.•^ There are some common threads among several of
  • One uses the mouse drag event to cause an action.actions. button is currently selected.help us redirect these events based on which•^ Thus, we really want to create a system that can

7

The State Pattern

to the current contained class.related contained classes, and pass method calls onan enclosing class switch between a number of•^ The State pattern is used when you want to have

Lets Use a State Object

the current state and executes methods on that state object.and put instances of all of them inside a StateManager class which sets•^ Then we’ll create 4 derived State classes for Pick, Rect, Circle and Fillclass.give our base class empty methods rather than creating an abstract base•^ Since none of the cases we’ve described need all of these events, we’ll•^ We’ll need a State object that handles mouse activities.

13

color to red.arbitrarily set the fillprogram, we have•^ In this simplefill color.color is the currentselected, and thenull if none isselected Drawing orthe currentlyargument is either^ •^ The Drawing

Switching Between States

the state is indicated by the button that is selected.states; we simply set the currentState variable todiscuss how the StateManager switches betweenwhen mouse events are sent to it, we need to•^ Now that we have defined how each state behaves

(^15)

Note…

number of resources.number of states which each consume a fair•^ This might be advisable if there are a largethese states on demand.•^ It would also be possible to use a Factory to createthe set methods are called.copy the correct one into the state variable wheninstance of each state during the constructor and•^ This version of the StateManager, we create an

17

The Rest of the Code…

to be called.•^ Instead, the correct state is already in place and its methods are ready•^ This is the critical piece -- there is no conditional testing.whichever state object is current.•^ The remainder of the state manager code simply calls the methods of

this state change takes place.•^ The beginning part of the Mediator illustrates howstate changes.tells the StateManager when the current program•^ The Mediator is the critical class, however, since itmanagement.the Mediator’s button and mouse event•^ It is clearer to separate the state management fromMediator and State Manager Interaction

19

Coding the Interaction

These startXxx (^) methods are invoked

button as a Command object.from the Execute methods of each

Concluding Remarks

less type checking, and, of course, greater chance of error.other languages, the states can be implemented by function pointers with muchall have common methods, although some of those methods can be empty. In•^ In Java, all of the States must inherit from a common base class, and they mustsimplifies and clarifies the program.•^ This approach generates a number of small class objects, but in the process,argument instead.Fill object has instance variables, and that color could easily be made an•^ State objects can be shared if they have no instance variables. Here only themakes the change explicit by copying one of the states to the state variable.state the program is in, and that may not always be checked correctly, this•^ It makes transition explicit. Rather than having a constant that specifies whichscattered through the program’s code.•^ It eliminates the necessity for a set of long, look-alike conditional statementseach state, and puts all the behavior for that state in a single object.•^ The State pattern localizes state-specific behavior in an individual class for