24-stateflyweightproxy

24-stateflyweightproxy - State, Flyweight & Proxy...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 24 — 11/12/2009 © University of Colorado, 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Goals • Cover Material from Chapters 10 and 11 of the Design Patterns Textbook • State Pattern • Bonus Pattern • Flyweight (not from textbook) • Proxy Pattern 2
Background image of page 2
State Pattern: Defnition • The state pattern provides a clean way for an object to vary its behavior based on its current “state” • That is, the object’s public interface doesn’t change but each method’s behavior may be different as the object’s internal state changes • DeFnition: The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. • If we associate a class with behavior, then • since the state pattern allows an object to change its behavior • it will seem as if the object is an instance of a different class each time it changes state 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
State Pattern: Structure op1() Context state.op1() op1() State state op1() ConcreteStateA op1() ConcreteStateB Look Familiar? 4
Background image of page 4
Strategy Pattern: Structure (from Lecture 16) performOperation() setAlgorithm(a: Algorithm) Client operation() Algorithm ConcreteAlgorithm1 strategy ConcreteAlgorithmN ... strategy.operation() Strategy and State Patterns: Separated at Birth?! Strategy and State are structurally equivalent; their intent however is different. Strategy is meant to share behavior with classes without resorting to inheritance; it allows this behavior to be conFgured at run-time and to change if needed; State has a very different purpose, as we shall see. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: State Machines for Gumball Machines Each circle represents a state that the gumball machine can be in. Each label corresponds to an event (method call) that can occur on the object 6 Has Quarter No Quarter Sold Gumball Out of Gumballs inserts quarter ejects quarter turns crank [gumballs = 0] dispense() [gumballs > 0] dispense()
Background image of page 6
Modeling State without State Pattern • Create instance variable to track current state • Defne constants: one For each state • ±or example • fnal static int SOLD_OUT = 0; • int state = SOLD_OUT; • Create class to act as a state machine • One method per state transition • Inside each method, we code the behavior that transition would have given the current state; we do this using conditional statements • Demonstration 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Seemed Like a Good Idea At The Time. .. • This approach to implementing state machines is intuitive • and most people would stumble into it if asked to implement a state machine for the Frst time • But the problems with this approach become clear as soon as change requests start rolling in • With each change, you discover that a lot of work must occur to update the code that implements the state machine • Indeed, in the Gumball example, you get a request that the behavior should change such that roughly 10% of the time, it dispenses two gumballs rather than one
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/11/2010 for the course CSCI 5448 taught by Professor Anderson during the Fall '09 term at Colorado.

Page1 / 50

24-stateflyweightproxy - State, Flyweight & Proxy...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online