srls01
srls01

Reputation: 613

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.

Upvotes: 2

Views: 190

Answers (1)

alex_noname
alex_noname

Reputation: 32073

Your task fits well in using the State pattern, below I presented an example code of how the class dependency might look. We split the big switch into several classes, each of which is responsible for processing one state and can transfer the program to other states.

For example, I used the Python language, since it is quite visual, and sketched out several classes similar to the problem I once solved, I also explicitly added methods that are called for states when switching to it and leaving it, sometimes this is useful.


class StateInterface:
    def on_enter(self):
        ...

    def work(self):
        ...

    def on_leave(self):
        ...


class StateOne(StateInterface):
    def __init__(self, main_program):
        self.main_program = main_program

    def on_enter(self):
        # do some init
        ...

    def work(self):
        # do some work
        new_state = StateTwo(self.main_program)
        self.main_program.change_state(new_state)

    def on_leave(self):
        # do some clear
        ...


class StateTwo(StateInterface):
    ...


class MainProgram:
    def __init__(self, initial_state: StateInterface):
        self.current_state = initial_state

    def infinite_loop(self):
        while not self.stopped:
            self.current_state.work()

    def change_state(self, new_state: StateInterface):
        self.current_state.on_leave()
        self.current_state = new_state
        self.current_state.on_enter()

Upvotes: 1

Related Questions