<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=State_pattern</id>
	<title>State pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=State_pattern"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=State_pattern&amp;action=history"/>
	<updated>2026-06-20T05:53:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=State_pattern&amp;diff=118966&amp;oldid=prev</id>
		<title>~2025-31915-51 at 18:33, 7 November 2025</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=State_pattern&amp;diff=118966&amp;oldid=prev"/>
		<updated>2025-11-07T18:33:36Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Software design pattern}}&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;state pattern&amp;#039;&amp;#039;&amp;#039; is a [[Behavioral pattern|behavioral]] [[software design pattern]] that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of [[finite-state machine]]s. The state pattern can be interpreted as a [[strategy pattern]], which is able to switch a strategy through invocations of methods defined in the pattern&amp;#039;s interface.&lt;br /&gt;
&lt;br /&gt;
The state pattern is used in [[computer programming]] to encapsulate varying behavior for the same [[object (computer science)|object]], based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to conditional statements and thus improve maintainability.&amp;lt;ref name=&amp;quot;GOF&amp;quot;&amp;gt;{{cite book |author=[[Erich Gamma]] |author2=[[Richard Helm]] |author3=[[Ralph Johnson (computer scientist)|Ralph Johnson]] |author4=[[John M. Vlissides]] |title=Design Patterns: Elements of Reusable Object-Oriented Software |publisher=[[Addison-Wesley]] |year=1995 |isbn=0-201-63361-2|title-link=Design Patterns: Elements of Reusable Object-Oriented Software }}&amp;lt;/ref&amp;gt;{{Rp|395}}&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
[[File:w3sDesign State Design Pattern UML.jpg|thumb|right|A sample UML class and sequence diagram for the State design pattern.&amp;lt;ref&amp;gt;{{cite web |title=The State design pattern – Structure and Collaboration |url=http://w3sdesign.com/?gr=b08&amp;amp;ugr=struct |website=w3sDesign.com |access-date=2017-08-12}}&amp;lt;/ref&amp;gt;]]&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;GoF&amp;quot;&amp;gt;{{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 }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The state pattern is set to solve two main problems:&amp;lt;ref&amp;gt;{{cite web |title=The State design pattern - Problem, Solution, and Applicability |url=http://w3sdesign.com/?gr=b08&amp;amp;ugr=proble |website=w3sDesign.com |access-date=2017-08-12}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* An object should change its behavior when its internal state changes.&lt;br /&gt;
* State-specific behavior should be defined independently. That is, adding new states should not affect the behavior of existing states.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* 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.&lt;br /&gt;
* A class delegates state-specific behavior to its current state object instead of implementing state-specific behavior directly.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Structure ==&lt;br /&gt;
[[File:State Design Pattern UML Class Diagram.svg|thumb|right|400px|State in UML&amp;lt;ref name=&amp;quot;GOF&amp;quot; /&amp;gt;]]&lt;br /&gt;
In the accompanying [[Unified Modeling Language]] (UML) [[class diagram]], the &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; class doesn&amp;#039;t implement state-specific behavior directly. Instead, &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; refers to the &amp;lt;code&amp;gt;State&amp;lt;/code&amp;gt; interface for performing state-specific behavior (&amp;lt;code&amp;gt;state.handle()&amp;lt;/code&amp;gt;), which makes &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; independent of how state-specific behavior is implemented. The &amp;lt;code&amp;gt;ConcreteStateA&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ConcreteStateB&amp;lt;/code&amp;gt; classes implement the &amp;lt;code&amp;gt;State&amp;lt;/code&amp;gt; interface, that is, implement (encapsulate) the state-specific behavior for each state. The UML [[sequence diagram]] shows the run-time interactions:&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; object delegates state-specific behavior to different &amp;lt;code&amp;gt;State&amp;lt;/code&amp;gt; objects. First, &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; calls &amp;lt;code&amp;gt;handle(this)&amp;lt;/code&amp;gt; on its current (initial) state object (&amp;lt;code&amp;gt;ConcreteStateA&amp;lt;/code&amp;gt;), which performs the operation and calls &amp;lt;code&amp;gt;setState(ConcreteStateB)&amp;lt;/code&amp;gt; on &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; to change context&amp;#039;s current state to &amp;lt;code&amp;gt;ConcreteStateB&amp;lt;/code&amp;gt;. The next time, &amp;lt;code&amp;gt;Context&amp;lt;/code&amp;gt; again calls &amp;lt;code&amp;gt;handle(this)&amp;lt;/code&amp;gt; on its current state object (&amp;lt;code&amp;gt;ConcreteStateB&amp;lt;/code&amp;gt;), which performs the operation and changes context&amp;#039;s current state to &amp;lt;code&amp;gt;ConcreteStateA&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
This is a [[C++]] example demonstrating the state pattern.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
using std::shared_ptr;&lt;br /&gt;
&lt;br /&gt;
// Abstract State&lt;br /&gt;
class FanState {&lt;br /&gt;
public:&lt;br /&gt;
    virtual void handleButtonPress(class Fan&amp;amp; fan) const = 0;&lt;br /&gt;
    virtual ~FanState() = default;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Context&lt;br /&gt;
class Fan {&lt;br /&gt;
private:&lt;br /&gt;
    shared_ptr&amp;lt;FanState&amp;gt; currentState;&lt;br /&gt;
public:&lt;br /&gt;
    explicit Fan(shared_ptr&amp;lt;FanState&amp;gt; state):&lt;br /&gt;
        currentState{state} {}&lt;br /&gt;
&lt;br /&gt;
    void setState(shared_ptr&amp;lt;FanState&amp;gt; state) noexcept {&lt;br /&gt;
        currentState = state;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void pressButton() noexcept {&lt;br /&gt;
        currentState-&amp;gt;handleButtonPress(*this);&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Concrete States&lt;br /&gt;
class OffState : public FanState {&lt;br /&gt;
public:&lt;br /&gt;
    void handleButtonPress(Fan&amp;amp; fan) const override {&lt;br /&gt;
        std::println(&amp;quot;Fan is OFF -&amp;gt; Switching to LOW speed.&amp;quot;);&lt;br /&gt;
        fan.setState(std::make_shared&amp;lt;LowSpeedState&amp;gt;());&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class LowSpeedState : public FanState {&lt;br /&gt;
public:&lt;br /&gt;
    void handleButtonPress(Fan&amp;amp; fan) const override {&lt;br /&gt;
        std::println(&amp;quot;Fan is on LOW speed -&amp;gt; Switching to HIGH speed.&amp;quot;);&lt;br /&gt;
        fan.setState(make_shared&amp;lt;HighSpeedState&amp;gt;());&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class HighSpeedState : public FanState {&lt;br /&gt;
public:&lt;br /&gt;
    void handleButtonPress(Fan&amp;amp; fan) const override {&lt;br /&gt;
        std::println(&amp;quot;Fan is on HIGH speed -&amp;gt; Turning OFF.&amp;quot;);&lt;br /&gt;
        fan.setState(make_shared&amp;lt;OffState&amp;gt;());&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    Fan fan(std::make_shared&amp;lt;OffState&amp;gt;());&lt;br /&gt;
&lt;br /&gt;
    // Simulate pressing the button several times&lt;br /&gt;
    fan.pressButton(); // OFF -&amp;gt; LOW&lt;br /&gt;
    fan.pressButton(); // LOW -&amp;gt; HIGH&lt;br /&gt;
    fan.pressButton(); // HIGH -&amp;gt; OFF&lt;br /&gt;
    fan.pressButton(); // OFF -&amp;gt; LOW&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Typestate analysis]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{Wikibooks|Computer Science Design Patterns|State|State implementations in various languages}}&lt;br /&gt;
&lt;br /&gt;
{{Design Patterns Patterns}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Software design patterns]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;/div&gt;</summary>
		<author><name>~2025-31915-51</name></author>
	</entry>
</feed>