Search code examples
design-patternsclass-design

Redesign using State Design Pattern -doubts


I want to redesign a code using state design pattern. The states belong to specific objects.

Example:

Class A can have three states: st1, st2, st3.

Similarly, another class, B, can have 2 states: st4, st5.

There is a case where a class C has one state: st1 and st4 and st5.

Basically these states should be interchangeable in case of complex objects.

Also, these classes A, B, and C contain data specific to them which will be used by the states.

My question is: How should the state machine be designed? Should there be individual SM for every class? Can all states have a common base interface?

Please help me out; you can ask me more questions if you need more info.


Solution

  • My question how should the state machine be designed ,should there be individual SM for every class ?

    If I understand your description, the answer is "yes", each object would require its own state machine.

    Can all states have a common base interface ?

    A common interface implies common API with multiple implementations. What common methods will each state have, and how do the implementations differ by state?

    You may not need a class for each state if there's no different behavior. You may just have a State class and a FSM that manages a collection of States and the rules that govern transitions.