Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
State pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Structure == [[File:State Design Pattern UML Class Diagram.svg|thumb|right|400px|State in UML<ref name="GOF" />]] In the accompanying [[Unified Modeling Language]] (UML) [[class diagram]], the <code>Context</code> class doesn't implement state-specific behavior directly. Instead, <code>Context</code> refers to the <code>State</code> interface for performing state-specific behavior (<code>state.handle()</code>), which makes <code>Context</code> independent of how state-specific behavior is implemented. The <code>ConcreteStateA</code> and <code>ConcreteStateB</code> classes implement the <code>State</code> interface, that is, implement (encapsulate) the state-specific behavior for each state. The UML [[sequence diagram]] shows the run-time interactions: The <code>Context</code> object delegates state-specific behavior to different <code>State</code> objects. First, <code>Context</code> calls <code>handle(this)</code> on its current (initial) state object (<code>ConcreteStateA</code>), which performs the operation and calls <code>setState(ConcreteStateB)</code> on <code>Context</code> to change context's current state to <code>ConcreteStateB</code>. The next time, <code>Context</code> again calls <code>handle(this)</code> on its current state object (<code>ConcreteStateB</code>), which performs the operation and changes context's current state to <code>ConcreteStateA</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)