Professional Documents
Culture Documents
Design Patterns
Patterns
CE00362-3
CT070-3-3
Intent:
Allow an object to alter its behaviour when its internal state
changes. The object will appear to change its class.
Motivation:
Many programmers have had the experience of creating a
class which performs slightly different computations or
displays different information based on the arguments
passed into the class. This frequently leads to some sort of
switch or if-else statements inside the class that determine
which behavior to carry out. It is this inelegance that the
State pattern seeks to replace.
Applicability:
Use State pattern should be used
• When an object’s behaviour depends on its
state, and it must change its behaviour at
run-time depending on that state.
• Operation have large, multipart conditional
statements that depend on the object’s
state.
CT070-3-3 - Design Patterns State Slide 6 (of 12)
Definition – State Pattern
Context State
request() handle()
ConcreteStateA ConcreteStateB
handle() handle()
Q&A