The state design pattern is a scheme used to model changes in the state (or states) of an object. It delegates rules for such changes to individual objects representing each possible state.
When should we Use the State Model?
An object with a relatively complex set of possible states applies the state model. Specially applied for an object with multiple business rules on how state transitions occur and what happens next.
In addition, you can update any state at any time with a special logic if the object is merely a state-owned property. This increases a needless complication in the State Schema. However, for the objects representing real world concepts, with a compound workflow, state structure can be a decent option.
What are Advantages of Using a State Design Pattern Model?
To begin with, a major advantage of the state model is its capability to minimize conditional complexity. It eliminates the need for if and switch statements on objects with different behavioral requirements, unique to different state transitions. So, representing an object’s state using a finite state machine diagram simplifies the conversion of the diagram into state design model’s types & methods.
What are the Disadvantages of Using this Model?
A developer needs to write a large amount of code for the state schema. Depending on the number of different defined state transition methods and possible object states, you can write numerous methods. Thus, for N states with M transition methods, the total number of methods required will be (N+1)*M.
An insurance policy with 5 different states and 5 methods for each (ignoring ListValidOperations method) requires 25 methods in total. The policy context type must also define the 5 state transition methods, increasing total methods required to 30.
Do you think this was a useful article for you? Also, feel free to provide your comments and concerns in the below comment section.