{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

24-stateflyweightproxy

24-stateflyweightproxy - State Flyweight Proxy Patterns...

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

View Full Document Right Arrow Icon
State, Flyweight & Proxy Patterns Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 24 — 11/12/2009 © University of Colorado, 2009 1
Image of page 1

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

View Full Document Right 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
Image of page 2
State Pattern: Definition • 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 • Definition: 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
Image of page 3

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

View Full Document Right Arrow Icon
State Pattern: Structure op1() Context state.op1() op1() State state op1() ConcreteStateA op1() ConcreteStateB Look Familiar? 4
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 configured at run-time and to change if needed; State has a very different purpose, as we shall see. 5
Image of page 5

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

View Full Document Right 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()
Image of page 6
Modeling State without State Pattern • Create instance variable to track current state • Define constants: one for each state • For example • final 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
Image of page 7

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

View Full Document Right 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 first 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 • Requires a change such that the “turns crank” action from the state “Has Quarter” will take you either to “Gumball Sold” or to “Winner” • The problem? You need to add one new state and update the code for each action 8
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern