lundi 25 janvier 2021

OOP design pattern for program refactoring

my problem is refactoring a program according to object-oriented programming principles. Program is running in a while loop endlessly and all operations in this main while loop. This main cycle has a switch-case statement. It has 11 cases and all cases are represent statements like unplanned_stop, planned_stop, read_data_from_x, read_data_from_y... Also, these states have if-else clauses in it to call different functions. Every state points to another state to the next step according to if-else decisions.

I have searched and State Design Pattern is seemed good for this solution but I am not sure. The main loop is like this:

while(programIsRunnning)
{
   switch(programState)
   {
      case state.StartOfLoop:
          if(..) doSomething();
          else doAnotherThing();
          programState = state.PlannedStop;
          break;
      case state.PlannedStop:
          if(..) programState = state.ReadDataFromX;
          else programState = state.ReadDataFromY;
      case state.ReadDataFromX:
          if(..) programState = state.UnplannedStop;
          else programState = state.StartOfLoop;
      .
      .
      .
  

I hope I could explain enough. This design is nasty and hard to understand to where to implement new specifications. For every new request for the program, I have to edit other states and if-else clauses. I am a junior and all can I think is recoding it with OOP design patterns. Any suggestions are welcome.

Aucun commentaire:

Enregistrer un commentaire