C# – Separation of concerns for a “Manager” class

cdesign-patternsseparation-of-concernssolid

I have a Class that "controls" the current application state, ApplicationStateManager.
I have an Enum that lists the possible states for the application

enum ApplicationState
{ 
    Idle,
    Starting,
    Started,
    Stopping,
    Stopped
}

ApplicationStateManager contains members such as:

public static ApplicationState CurrentState = ApplicationState.Idle;

public static void ChangeCurrentState(ApplicationState newState)
{
      CurrentState = newState;
}

I could query the CurrentState static field by using ApplicationStateManager.CurrentState; to see if I can continue code execution or something along these lines.

Now the question is I was trying to separate concerns by having an ApplicationStateChanger but the problem is, where to stick the CurrentState variable?

Best Answer

If the behavior of your application depends on the state it is in, then this is a clear indicator that you might want to implement it as a Finite State Machine.

This can be achieved using State Pattern or Visitor Pattern. You should use former if the number of states is prone to change more often, and the latter if the number of events that can trigger state change is prone to change more often.

Related Topic