Design Patterns - State

Software Engineering
1.0x

Design Patterns - State

Created 3 years ago

Duration 0:07:06
lesson view count 510
Select the file type you wish to download
Slide Content
  1. Design Patterns: State

    Slide 1 - Design Patterns: State

    • Emerson Murphy-Hill
  2. State Pattern

    Slide 2 - State Pattern

    • Intent:
    • Provide the ability for an object to change its behavior in response to internal state changes.
    • Example:
    • ©Bob Tarr
  3. State Pattern: Applicability

    Slide 3 - State Pattern: Applicability

    • Use the State pattern whenever:
    • An object's behavior depends on its state, and it must change its behavior at run-time depending on that state
    • Operations have large, multipart conditional statements that depend on the object's state. The State pattern puts each branch of the conditional in a separate class.
    • ©Bob Tarr
  4. Example Not using State Pattern

    Slide 4 - Example Not using State Pattern

    • Consider a class that has two methods, push() and pull(), whose behavior changes depending on the state of the object
    • To send the push and pull requests to the object, we'll use the following GUI with "Push" and "Pull" buttons
    • The state of the object will be indicated by the color of the canvas in the top part of the GUI
    • The states are: black, red, blue and green
    • ©Bob Tarr
  5. Example Not using State Pattern

    Slide 5 - Example Not using State Pattern

    • ©Bob Tarr
  6. Example Not using State Pattern

    Slide 6 - Example Not using State Pattern

    • ©Bob Tarr
    • Lots of Messy Conditionals!
  7. Example Not using State Pattern

    Slide 7 - Example Not using State Pattern

    • ©Bob Tarr
    • Duplicated conditions!
  8. Example using State Pattern

    Slide 8 - Example using State Pattern

    • Now let's use the State pattern!
    • Here's the class diagram:
    • ©Bob Tarr
  9. Example using State Pattern

    Slide 9 - Example using State Pattern

    • First, we'll define the abstract State class:
    • Next, we'll write concrete State classes for all the different states: RedState, BlackState, BlueState and GreenState
    • ©Bob Tarr
  10. Example using State Pattern

    Slide 10 - Example using State Pattern

    • For example, here's the BlackState class:
    • ©Bob Tarr
  11. Example using State Pattern

    Slide 11 - Example using State Pattern

    • And, here's the new Context class that uses the State pattern and the State classes:
    • ©Bob Tarr
  12. Example using State Pattern

    Slide 12 - Example using State Pattern

    • ©Bob Tarr
  13. Differences between Strategy and State pattern

    Slide 13 - Differences between Strategy and State pattern

    • State pattern changes the behavior of the client by progressing through a set of states, while strategy pattern applies a specific strategyThe client may not be aware of the changes in state in case of the state pattern.In case of the strategy pattern, the client is aware of the strategy it would like to apply in a certain context.The state pattern is used to handle an ordered set of operations (eg: transitions in a Finite State Machine).