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
5
votes
3 answers

State pattern java

I am learning design pattern in java I was doing through some of links.I am trying to design a washing machine by state pattern I have a query regarding the implementation of state design pattern public interface State { public void openLid(); …
coder25
  • 2,363
  • 12
  • 57
  • 104
5
votes
2 answers

State Pattern share common knowledge between state object efficient and extandable implementation

I am using the State Pattern, but the examples I found are for educational purpose and I wanted to know what are best practices in this pattern for sharing objects between states, i.e. a boolean, a List and also for one state object to change the…
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
5
votes
2 answers

Transition methods in state design pattern

I have a state machine with many states A--B--C--D--E. I have many transitions from C for example to A if some condition is verified. For every state I have a class extending abstract class Stateand I have a manager that delegates every transition…
Matroska
  • 6,885
  • 14
  • 63
  • 99
5
votes
2 answers

State pattern: Why doesn't the context class implement or inherit the State abstract interface/class?

I'm reading about the State pattern. I have only just begun, so of course I begin by reading the entire Wikipedia article on it. I noticed that both of the examples in the article have some base abstract class or Java interface for a generic State's…
Ricket
  • 33,368
  • 30
  • 112
  • 143
5
votes
2 answers

To use or not to use the State Pattern?

I'm designing a pinball game for a Uni project in which there are supposed to be 2 modes: running mode and builder mode, whereby one can design/redesign the layout of the machine. My initial thought was the State pattern - however, I'm concerned…
Robert
  • 8,406
  • 9
  • 38
  • 57
5
votes
1 answer

State Pattern with Entity Framework

I have a model Enquiry, which can be in one of two states (there are more but for the purposes of this I will just compare two): New and Closed. The state the enquiry is in is dependant upon what a user is able to do with an enquiry. For example a…
CallumVass
  • 11,288
  • 26
  • 84
  • 154
4
votes
5 answers

How should an object using the state pattern be transitioned to the next state?

I have an Order class which goes through a series of defined states. To aid with this, I have implemented the State pattern such that the Order object has a CurrentState member which implements an IOrderState interface. I then have concrete…
James
  • 720
  • 9
  • 19
4
votes
0 answers

What is the best way to implement the State design pattern in Django model layer?

We have some Tasks and states, that a task might have. You may think of any task tracking app, aka redmine. States are constant and won't change in the future. The possibilities of transition from one state to another are also constant. class…
4
votes
2 answers

State or observer pattern in object C++

I am just a bit confused with the difference between the observer and state pattern. I have been given a project where the client is an airplane on a flight which calculates data for different sensor such as GPS,Speed, Fuel level, And then the data…
4
votes
1 answer

State design pattern in compliance with Liskov

I am designing an order system and the state design pattern seems appropriate because the order can change its state and thereby the allowed functionalities for the order. Below is my basic class diagram: I do not like this approach because clients…
4
votes
1 answer

Best practice: Design pattern for 2D HUD screen navigation

If you have an application with a GUI totally working on 2D drawing, what should be the best practice to handle what to draw and where to touch? An example for better understanding: I have a game with a map. On this map I can build houses and…
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
4
votes
2 answers

Is the state design pattern overkill for a PHP form?

I have a page that, when accessed displays a table of information related to a video: Embed code Title Description Current gallery Thumbnail image This information is read-only when the page is first accessed. There is a select menu that has the…
Mike Moore
  • 7,228
  • 10
  • 42
  • 63
4
votes
2 answers

Alternative for instanceOf (State Pattern)

I'm having a problem using the state pattern, I don't know how to check if a State is of a certain instance without using instanceOf (because that is considered a bad practice). TCPConnection holds a TCPState object. Let's say I want to get all the…
Stanko
  • 4,275
  • 3
  • 23
  • 51
4
votes
5 answers

State Pattern with Memory

I was using State Pattern in a normal state machine. I wanted to be able to go from [A -> B], [B -> C], and [A -> C]. Now our domain has a new rule, now i need to go from [C -> A] also,but only if i have never been in B before. So we have states…
Miguel Marques
  • 2,386
  • 1
  • 20
  • 31
4
votes
1 answer

State Pattern - Django models

I'm currently trying to implement the state pattern in Django. Take these models for example: class Restaurant(models.Model): name = models.CharField() # other fields here ... class State(models.Model): pass class StateOpen(State): …
XuoriG
  • 600
  • 4
  • 17
1 2
3
14 15