CS计算机代考程序代写 State_Pattern

State_Pattern

COMP2511
State Pattern

Prepared by
Dr. Ashesh Mahidadia

State Pattern

These lecture notes are from the wikipedia page at: https://en.wikipedia.org/wiki/Finite-state_machine

And

the reference book “Head First Design Patterns”.

2COMP2511: State Pattern

Finite-state Machine
• A finite-state machine (FSM), is an abstract machine that can be in exactly one of a
finite number of states at any given time.

• the finite-state machine can change from one state to another in response to some
external inputs.

• the change from one state to another is called a transition.

• An finite-state machine is defined by
• a list of its states

• the conditions for each transition

• its initial state

• Finite-state machine also refer to as finite-state automaton, finite automaton, or
state machine

COMP2511: State Pattern 3

COMP2511: State Pattern 4

Example: coin-operated turnstile

State Transition Table: shows for each possible state, the transitions between them
(based upon the inputs given to the machine) and the outputs resulting from each input

COMP2511: State Pattern 5

State Machines: Simple examples

• vending machines, which dispense products when the proper
combination of coins is deposited,

• elevators, whose sequence of stops is determined by the floors
requested by riders,

• traffic lights, which change sequence when cars are waiting,

• combination locks, which require the input of combination numbers
in the proper order.

COMP2511: State Pattern 6

State Machine: Terminology
• A state is a description of the status of a system that is waiting to execute
a transition.
• A transition is a set of actions to be executed when a condition is fulfilled or
when an event is received.
• Identical stimuli trigger different actions depending on the current state.
• For example,

• when using an audio system to listen to the radio (the system is in the “radio” state),
receiving a “next” stimulus results in moving to the next station.

• when the system is in the “CD” state, the “next” stimulus results in moving to the next track.

• Often, the following are also associated with a state:
• an entry action: performed when entering the state, and
• an exit action: performed when exiting the state.

COMP2511: State Pattern 7

Representations

• The most common representation is shown below:

COMP2511: State Pattern 8

State Machines for UI

• Examples …

COMP2511: State Pattern 9

COMP2511: State Pattern 10

From the reference book
“head First Design Patterns”

Gumball Machine!

COMP2511: State Pattern 11

From the reference book
“head First Design Patterns”

Read the example
code provided for

this week

COMP2511: State Pattern 12

From the reference book
“head First Design Patterns”

Read the example
code provided for

this week

COMP2511: State Pattern 13

From the reference book
“head First Design Patterns”

Read the example
code provided for

this week

COMP2511: State Pattern 14

From the reference book
“head First Design Patterns”

COMP2511: State Pattern 15

From the reference book
“head First Design Patterns”

Demo …

• Demo of Gumball, from the reference book “Head First Design
Patterns”.

COMP2511: State Pattern 16

COMP2511: State Pattern 17

From the reference book
“head First Design Patterns”