



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Memon; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




1
3
program should exhibit.editor affects the behavior the
managing the actions of 5 command buttons.•^ We might design our program with a Mediator
5
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
7
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
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
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)
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
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
19
These startXxx (^) methods are invoked
button as a Command object.from the Execute methods of each
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