Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 hardly maintainable and understandable chunks of code. I'm considering refactoring one of these, but I'm having trouble finding good resources for this online. There are many simple examples of State Pattern online, but I need some more in depth resources.

So I'm looking for:

  • Examples of common pitfalls when implementing state pattern and how to avoid them,
  • Real world examples of state pattern done correctly (like in some open source project/framework)
  • Personal experiences with state pattern are also welcome

Thank you for your time

like image 645
Ivan Avatar asked Feb 08 '11 16:02

Ivan


People also ask

Why do we use state patterns?

The state pattern is used in computer programming to encapsulate varying behavior for the same 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.

What is true about state design pattern?

State design pattern is used when an Object changes its behavior based on its internal state. If we have to change behavior of an object based on its state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state.

What is a difference between the State pattern and the strategy pattern?

State pattern helps a class to exhibit different behaviors in a different state. Strategy Pattern encapsulates a set of related algorithms and allows the client to use interchangeable behaviors through composition and delegation at runtime.


1 Answers

@Ivan: There are a number of resources available on the web for Hierarchical State Machines (HSM). Miro Samek has written extensively about this design pattern and offers a lot of helpful information.

Some articles that should be of interest:

  • State-Oriented Programming (SOP) (Samek) (pdf)
  • Hierarchical State Machines - a Fundamentally Important Way of Design (Samek) (pdf)
  • Practical State Charts in C/C++ (Samek) (link to google-books)
  • State-Oriented Programming (by Asher Sterkin) (pdf)

The big benefit of using HSM over flat FSM state charts described by Mealy and Moore is that the hierarchy creates a separation of responsibility. Sub-states only need to handle those conditions that they are expressly designed to handle--unhandled events get passed up to the parent state, if the parent state isn't expressly designed to handle it then it is passed up to the next-higher parent and so on. It allows you to create small, manageable state machines that each serve a single purpose--one that can fit within a single object. As new features are added, or as new classes are added, they only need to handle their own little part of the world and pass on the unhandled events to their respective parents.

When implemented correctly, you get a robust program with low cyclomatic complexity, that is easy to modify or upgrade as needed.

like image 169
oosterwal Avatar answered Sep 21 '22 06:09

oosterwal