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
0
votes
1 answer

how to implement state pattern using ddd

I have a question about the state pattern and how to implement it using DDD. For what I understand of the state pattern, the pattern is used to handle the transition between different states, so its the responsibility of each state to handle what…
Juan Vega
  • 1,030
  • 1
  • 16
  • 32
0
votes
2 answers

State pattern: Decoupling the MoveNext() method

I've had a go at implementing the state pattern with a bit of help from the following StackOverflow post: state pattern Good so far. I can now move the object(the document, to the next state, that of ACKNOWLEDGED.) public override void…
Chris Yoker
  • 61
  • 1
  • 6
0
votes
0 answers

When using state-pattern in XML parsing, Is it wise to unit test different "state" classes

I am writing unit tests and want to test parsing of an XML document. To parse not too complicated XML document I use standard NSXMLParser and state pattern. I will test proper parsing of a document by checking result object created by…
MichK
  • 3,202
  • 3
  • 29
  • 33
0
votes
2 answers

State pattern implementation hardship

I use state pattern in my application because my state-entities has API the behaviour of which changes due to current state. In my case that state-dependent API consists only from one method - changeState. This method change state and also execute…
VB_
  • 45,112
  • 42
  • 145
  • 293
0
votes
1 answer

Can I solve this Card Game App with a State Design Pattern?

I've been at this for quite a while and I can't seem to solve the dynamic of the game. So, basically, the game is a really popular card game in my Country called Truco. I'll try to explain it with the following bullet points, but anyways, the Bold…
0
votes
2 answers

state pattern long state class names

I am using state pattern on 28 states in my application, the states are for membership cards that has 7 major states, there are 4 boolean attributes for the membership card that actually affects the its behavoir so i have decided to embed them with…
Sisyphus
  • 900
  • 12
  • 32
0
votes
1 answer

Common behavior in State Pattern states

You have an ATM Machine with 3 states and 2 methods. if this is a phesudo implemntation of the pattern. 01-- class AbstractATMState 02-- Operation1 03-- Operation2 04-- 05-- class State1 : AbstractATMState 06-- Operation1 07-- Operation2 08-- 09--…
Sisyphus
  • 900
  • 12
  • 32
0
votes
1 answer

state pattern: does it make sense to use it if each state is only allowed to perform just 1 or 2 actions?

1) The extreme case: I have 10 states and 10 possible actions to perform (that result in state transitions). But each of these actions are only allowed in their particular state. State 1 can only perform action 1, State 2 action 2, and so on (a…
Pato Loco
  • 1,205
  • 1
  • 13
  • 29
0
votes
3 answers

combining decorator and state pattern in java - question about OO design

I am in the middle of solving a problem where I think it's best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set amount of ingredients and a few different types…
denchr
  • 4,142
  • 13
  • 48
  • 51
0
votes
1 answer

What's the difference between State, IntegerState, ExplicitIntegerState and ImplicitIntegerState?

I'm familiar with the state pattern, but recently I've heard about the three other design patterns mentioned in the title. I have no idea what distinguishes those three patterns from the normal state pattern, and Google also doesn't help! UPDATE:…
vauge
  • 801
  • 4
  • 14
  • 30
0
votes
1 answer

Implementing State Pattern using references

I'm trying to refactor my code, among other things, applying state pattern. I'm more of a Java programmer, so please, be nice ;) So, here I've got my base state class, nothing fancy: #include #include…
0
votes
2 answers

state design pattern from Java to Ruby

I have a working solution in java using a classic state design pattern and facing some difficulties translating it to ruby. I am new in ruby, but the diffuclty I believe lies in the differences on how patterns can be implemented in dynamic…
denchr
  • 4,142
  • 13
  • 48
  • 51
0
votes
1 answer

What is an example of state pattern in Core Java/JSF/Servlets?

I just learnt about the State pattern . I would like to know where in core Java / JSF/Servlets code s can I see this pattern being implemented ? I want to see how the state transition is implemented and who plays the role of states and who plays…
Geek
  • 26,489
  • 43
  • 149
  • 227
0
votes
2 answers

The "state design pattern" could be considered an "architectural pattern"?

"An architectural pattern expresses a fundamental structural organization schema for a software system". There are software systems that can be described as finite-state machines (specially many real-time systems and embedded systems), if we…
Abel Morelos
  • 1,228
  • 5
  • 20
  • 31
0
votes
1 answer

State pattern misuse

I am trying to create a simple flow from state to state (6 or 7 in all) and I decided to implement a state pattern. It felt like there was too much overhead around letting each state transition to each state when really there should be a main flow.…
Tyler Smith
  • 1,269
  • 1
  • 12
  • 24
1 2 3
14
15