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
4
votes
2 answers

Difference between State pattern and Strategy pattern

Looking at the GoF patterns I find the similarities between State and Stategy pattern rather striking. Both swap out polymorphic classes to modify behavior. Anyone else found the same? What are the exact differences?
user2657232
4
votes
2 answers

Using a 'variation' on State Pattern - C++

Overview: I am trying to improve the design of a program that I am using state pattern for. I will post a brief description of the problem, an image of the class diagram/description of the current design, followed by header code for the relevant…
simplyletgo
  • 151
  • 5
4
votes
5 answers

Strategy Design pattern vs State Design pattern

I was reading through this link about the state pattern. Is it looks like strategy pattern? What is the exact difference between these two patterns?
Krishna
  • 7,154
  • 16
  • 68
  • 80
3
votes
2 answers

State and FlyWeight patterns

Can anybody share example of using state pattern with flyweight pattern (flyweight pattern is for creating state objects to save memory)? UPDATE: How to use a combination of state and fw patterns?
drifter
  • 683
  • 1
  • 11
  • 24
3
votes
1 answer

How to apply the State Pattern with Dart enhanced enums

Dart 2.17 now has enhanced enums. Matt Carroll mentioned in his review that you could use the new enums to implement the State Pattern. I'm having trouble with this because I'm weak on both the State Pattern and on enhanced enums. Here is an example…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
3
votes
1 answer

Broken encapsulation in State pattern in Java

I have implemented state pattern for my socket library in Java. I simplified code to reduce size and increase readability just to demonstrate the problem. I don't know how to encapsulate using of Socket class by states classes. I've written comments…
zenno2
  • 425
  • 2
  • 7
3
votes
2 answers

Using The State Pattern in games

Recently, I've tried to create Snake game in SFML. However, I also wanted to use some design pattern to make some good habits for future programming - it was The State Pattern. But - there is some problem that I am unable to solve. To make…
3
votes
2 answers

Why is proxy pattern Structural Pattern and why is State Pattern behavioral pattern?

why is proxy pattern a structural pattern and why is state pattern a behavioral pattern. What is the criteria for determining a new pattern should be considered structural or behavioral?
Anjana
  • 31
  • 2
3
votes
0 answers

State-dependent behaviour of Python objects using Ruby-like eigenclasses with mixins

I've been looking for a natural way to implement state-dependent behaviour (state machines) in Python objects. The goal was for objects to have a small number of states, or "mutually orthogonal" aspects of state, which would determine their…
Alexey
  • 3,843
  • 6
  • 30
  • 44
3
votes
4 answers

Why State Design Pattern over If-else

Everywhere I find people saying it is better to use state design pattern over if else. I want to know why: It is an improvement to if-else in real world use case? Does it make code more testable? can any one please provide an example where state…
Nikhil Kamani
  • 850
  • 9
  • 12
3
votes
2 answers

State Pattern in Python

I am having some issues wrapping my head around on implementing the state design pattern in Python. I am new to Python and wrote some code to try and answer this question that was presented to me: Write the code for a simple ATM that allows a user…
Paresa
  • 31
  • 1
  • 2
3
votes
1 answer

Fluent NHibernate Mapping - State Pattern

I'm not too hot on NHibernate / FNH Mapping but I am looking at implementing the state pattern and like the idea of Derick Bailey's article here: I beleive this was a while ago so the mapping code is out of date, can someone give me a hand to update…
3
votes
2 answers

GOF State Pattern State Transition Implementation Issues

Firstly, can anyone explain how a state object can be shared when the state object has no instance variables ? This text is taken from GOF, page 308, item 3 (consequences section): The state object can be shared. If state objects have no…
nicholas
  • 2,581
  • 14
  • 66
  • 104
3
votes
4 answers

get objects with a certain state using state pattern

I was assigned to design a piece of software (in java) with projects and jobs where jobs each have a status. As we learned about the GoF-patterns, the State pattern seemed like an obvious choice to me, but now I'm having some troubles with the…
3
votes
1 answer

Implementing State Pattern

I am implementing a state pattern in Java for my application and need few clarifications. The state machine has 5 states State 1 to State 5. The are a total of 5 events(Event1 to Event5) which causes the state transition. Not all events are…
Arun
  • 327
  • 1
  • 3
  • 12