Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
State pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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!
==Overview== [[File:w3sDesign State Design Pattern UML.jpg|thumb|right|A sample UML class and sequence diagram for the State design pattern.<ref>{{cite web |title=The State design pattern β Structure and Collaboration |url=http://w3sdesign.com/?gr=b08&ugr=struct |website=w3sDesign.com |access-date=2017-08-12}}</ref>]] The state design pattern is one of twenty-three [[Design Patterns|design patterns documented by the Gang of Four]] that describe how to solve recurring design problems. Such problems cover the design of flexible and reusable object-oriented software, such as objects that are easy to implement, change, test, and reuse.<ref name="GoF">{{cite book |author=Erich Gamma |author2=Richard Helm |author3=Ralph Johnson |author4=John Vlissides |title=Design Patterns: Elements of Reusable Object-Oriented Software |year=1994 |publisher=Addison Wesley |isbn=0-201-63361-2 |pages=[https://archive.org/details/designpatternsel00gamm/page/305 305ff] |url-access=registration |url=https://archive.org/details/designpatternsel00gamm/page/305 }}</ref> The state pattern is set to solve two main problems:<ref>{{cite web |title=The State design pattern - Problem, Solution, and Applicability |url=http://w3sdesign.com/?gr=b08&ugr=proble |website=w3sDesign.com |access-date=2017-08-12}}</ref> * An object should change its behavior when its internal state changes. * State-specific behavior should be defined independently. That is, adding new states should not affect the behavior of existing states. Implementing state-specific behavior directly within a class is inflexible because it commits the class to a particular behavior and makes it impossible to add a new state or change the behavior of an existing state later, independently from the class, without changing the class. In this, the pattern describes two solutions: * Define separate (state) objects that encapsulate state-specific behavior for each state. That is, define an interface (state) for performing state-specific behavior, and define classes that implement the interface for each state. * A class delegates state-specific behavior to its current state object instead of implementing state-specific behavior directly. This makes a class independent of how state-specific behavior is implemented. New states can be added by defining new state classes. A class can change its behavior at run-time by changing its current state object.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
State pattern
(section)
Add topic