Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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? –  user2080257 Sep 11 '13 at 12:05

1 Answer 1

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;
            break;
          }
        }
        if(matched == true)
        {
           next_state = state;
           break;
        }
    }
    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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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