简体   繁体   English

OOP 用于程序重构的设计模式

[英]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.程序无休止地在一个while循环中运行,所有操作都在这个主while循环中。 This main cycle has a switch-case statement.这个主循环有一个 switch-case 语句。 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.它有 11 种情况,所有情况都是代表语句,如unplanned_stopplanned_stopread_data_from_xread_data_from_y ......此外,这些状态中有 if-else 子句来调用不同的函数。 Every state points to another state to the next step according to if-else decisions.根据 if-else 决策,每个 state 指向另一个 state 到下一步。

I have searched and State Design Pattern is seemed good for this solution but I am not sure.我已经搜索过,State 设计模式似乎适合这个解决方案,但我不确定。 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.对于程序的每个新请求,我都必须编辑其他状态和 if-else 子句。 I am a junior and all can I think is recoding it with OOP design patterns.我是一名大三学生,我所能想到的就是用 OOP 设计模式对其进行重新编码。 Any suggestions are welcome.欢迎任何建议。

Your task fits well in using the State pattern , below I presented an example code of how the class dependency might look.您的任务非常适合使用State 模式,下面我提供了一个示例代码,说明 class 依赖项的外观。 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.我们把大交换机拆分成几个类,每个类负责处理一个state,可以将程序转移到其他状态。

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.例如,我使用了 Python 语言,因为它非常直观,并且勾勒出了几个类似于我曾经解决的问题的类,我还明确添加了在切换到它时调用状态的方法和离开它,有时这很有用.


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM