I am using state pattern on 28 states in my application. The states are for membership cards that has 7 major states, there are 4 boolean attributes for the membership card that actually affects the its behavior so I have decided to embed them with states, that's how it multiplied to 28 states.

The problem now is with states class naming, it is getting crazing, I am ending up with class state named like this Membership-UnderCreation-Printed-Linked-Premium-Frozen ----- I have hyphened different attributes to make it clear.

Is it ok for state class names to be like this?

  • 1
    Are you sure that is a sensible use of state pattern? – Jan Hudec Sep 11 '13 at 10:53
  • i have an entity (membership card) that changes its behavior (Freeeze(), Unfreeze(), ReportLost() etc) according to its internal state, any other pattern suitable for this? – Sisyphus Sep 11 '13 at 12:05

It is not the strategy I personally would have chosen. I would have defined the 7 states, and defined the 4 attributes as properties of the application object. You then have to define business rules that apply to the application based on these attributes that may apply changes of state to the application.

The benefit of this approach is that additional business rules are implemented in code as additional business rules which is very maintainable.

In your design, you are encoding business rules into the state machine, which means that over time, as business rules are introduced the state machine grows and becomes completely unmaintainable.

As an example:

public decide_next_state(IApplication application,  Dictionary<state, List<IBusinessRules>> rules)
    //  by default we remain in the current state.
    next_state = application.current_state

    foreach(state in rules.keys())
        bool matched = true;
        foreach(rule in rules[state])
          //  if this business rule blocks the transition, then it
          //  returns false.
          if rule.apply(application) == false
            matched = false;
        if(matched == true)
           next_state = state;
    return next_state;

Now with this code, you would pass in the rules dictionary for the applications current state. The rules then define which state the current application is will transition to. However it can only make that transition if all the business rules for that transition allow it.

This is a very simple model, but it can be made much more complex if you need to.

  • i am not sure what do you mean exactly, could you kindly explain more, may be with small example? :) – Sisyphus Sep 11 '13 at 13:34
  • yep, will do. But will have to wait until after I finish work this evening – Michael Shaw Sep 11 '13 at 13:38
  • sure, i will be waiting, thanks for your kind efforts :) – Sisyphus Sep 11 '13 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.