lecture9_the_state_pattern.pdf - CSC3380 Object Oriented Design Lecture 9 The state pattern The Quiz Quiz Answer Writing Code(The lesson of the

lecture9_the_state_pattern.pdf - CSC3380 Object Oriented...

This preview shows page 1 - 13 out of 43 pages.

CSC3380 Object Oriented Design Lecture 9: The state pattern
Image of page 1
The Quiz
Image of page 2
Quiz Answer
Image of page 3
Writing Code (The lesson of the CoolCam™)
Image of page 4
The Project Grade is multiplied by the participation score A 0% participation score means a 0% project grade A 50% participation score and a 80% project grade means a personal score of 40%.
Image of page 5
Questions?
Image of page 6
This class The state pattern
Image of page 7
Motivating example Suppose we’re developing a video game… We have the following “states”: Main menu Character Select In-game Each state has dramatically different behavior! (Also applies to simulations and many servers)
Image of page 8
The basics: Every game and simulation has a main loop The main loop runs multiple times per second to maintain the game: Check for button presses Update the interface Process animations Move objects / process physics etc.
Image of page 9
Structured Approach Giant switch-case or if-then statement inside of main loop: void update() { if ( currentState == MAIN_MENU ) { drawMainMenu(); checkMenuButtonClicked(); } else if ( currentState == IN_GAME ) { drawCharacters(); drawInterface(); getCurrentMove(); } }
Image of page 10
What’s wrong? What’s wrong with having a single switch-case to perform updates?
Image of page 11
Issues The states should be totally independent, but they exist in the same function.
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 43 pages?

  • Spring '09
  • Suptik

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors