Questions tagged [state-pattern]

Use this tag for questions relating to the State design pattern, one of the Gang of Four's behavioral design patterns. Also consider using the [design-patterns] tag and a programming language tag if applicable.

According to the GoF book (page 305) the purpose of the State design pattern is to,

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

There are two scenarios where the State design pattern is applicable (page 306).

  1. An object's behavior depends on its state, and it must change its behavior at run-time depending on that state.
  2. Operations have large, multipart conditional statements that depend on the object's state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects.

There are three consequences of applying the State design pattern (page 307).

  1. It localizes state-specific behavior and partitions behavior for different states.
  2. It makes state transitions explicit.
  3. State objects can be shared.

For details about the structure and implementation of the State design pattern, see the following online resources.

Note the tag encompasses this pattern as well as the other 22 patterns from the GoF book. Consider using any of these tags in combination, as applicable.

213 questions
271
votes
21 answers

What is the difference between Strategy design pattern and State design pattern?

What are the differences between the Strategy design pattern and the State design pattern? I was going through quite a few articles on the web but could not make out the difference clearly. Can someone please explain the difference in layman's…
Chin Tser
  • 2,799
  • 2
  • 18
  • 6
64
votes
8 answers

How to implement a FSM - Finite State Machine in Java

I have something to do for work and I need your help. We want to implement a FSM - Finite State Machine, to identify char sequence(like: A, B, C, A, C), and tell if it accepted. We think to implement three classes: State, Event and Machine. The…
Ofir A.
  • 3,112
  • 11
  • 57
  • 83
49
votes
7 answers

What is the difference between a state machine and the implementation of the state pattern?

I wonder if a state machine is just the state pattern at work or if there is actually a difference between those two? I found this article with the bold title "the state design pattern vs state machine" but at the end of the day he only says that…
Christoph
  • 26,519
  • 28
  • 95
  • 133
47
votes
8 answers

How to use state pattern correctly?

I've encountered a few implementations of state pattern in my programming experience, and done a few. I've seen them used in various scenarios (mostly UI and parsing). The trouble is that all of them under pressure of rapid development turned into…
Ivan
  • 1,735
  • 1
  • 17
  • 26
16
votes
5 answers

How to persist objects which implement the State pattern?

I am new to the State design pattern and I can't find a proper example of saving different states of an object to the database (SQL Server in my case). The scenario is quite similar [almost identical] to the example described in the following…
MHOOS
  • 5,146
  • 11
  • 39
  • 74
16
votes
2 answers

Persisting the state pattern using Entity Framework

I am currently developing a project in MVC 3. I've separated my concerns so there are projects such as Core, Repository, UI, Services etc. I have implement the Repository, UnitOfWork and most importantly the State pattern. I am using Entity…
16
votes
2 answers

Who defines state transitions in the state pattern?

I understand that the State pattern can be used to model objects that changes behavior depending on the state and the various states that the Context can have is encapsulated in concrete classes that represent a State interface . What I am not clear…
Geek
  • 26,489
  • 43
  • 149
  • 227
14
votes
2 answers

State pattern vs ENUM

From time to time it's need to make support for states for objects. As I understand there are two approaches: ENUM (SIMPLE) STATE pattern (OC principle) it's evident that need to use State pattern for such purposes(I am not sure). But reading…
user1074896
  • 1,025
  • 2
  • 15
  • 27
13
votes
3 answers

State pattern and guard

Update: State pattern might a wrong way to solve this. Hence, any other pattern is welcome. Basically I'm looking for a way to have guard conditions for each state yet having a clean and maintainable code. How would front-end side routing systems…
Sam R.
  • 16,027
  • 12
  • 69
  • 122
12
votes
1 answer

How to unit test a state machine?

Suppose I have an Order class, which can be in three different states : CheckedState, PaidState and OrderedState. The state machine will be implemented using the standard State Design Pattern (Gof). How do you usually unit test this? Do you use a…
Riana
  • 689
  • 6
  • 22
10
votes
5 answers

State Pattern: How the states of an object should transition when they're involved in complex processes?

I have some doubts about the following implementation of the state pattern: I have an Order object. For simplicity, let's suppose it has a quantity, productId, price and supplier. Also, there are a set of known states in which the order can…
nick2083
  • 1,953
  • 13
  • 16
10
votes
1 answer

The State Pattern seems to use a circular reference. Why is that okay?

I'm still trying to understand the dangers of circular references. I often read that they should only be used in rare cases. But, in the canonical State Pattern, the "state" objects need to reference the "context" object in order to cause a…
Pup
  • 10,236
  • 6
  • 45
  • 66
9
votes
3 answers

State Pattern C# with previous states

I am new to the state pattern implementation in C#, could you provide some info on how you implement it. I am refactoring a state machine in C# using the state pattern. Currently my state machine contains 5 states and it is only possible to go…
Manolete
  • 3,431
  • 7
  • 54
  • 92
8
votes
2 answers

State Pattern in ASP.NET MVC 3.0

I have a registration page in my application. It has 3 states and 1 error state(If any error comes): Fill Basic Information Select Package Say Thanks Error Now I want to use state pattern here. First I created a console application which is OK.…
user946393
8
votes
2 answers

state pattern C++

I'm trying to instigate a simple State pattern, after following some of the excellent tutorials here: http://gameprogrammingpatterns.com/state.html I am half way through this current tutorial, and I am trying to replicate the static instances of…
1
2 3
14 15