Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c# - 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.

question from:https://stackoverflow.com/questions/65885510/oop-design-pattern-for-program-refactoring

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...