For example State Mode. This mode and Strategy Patterns have certain similarity:
InStateMode, the derived class holds a reference pointing back to the context class. The main function of the derived class is to use this reference to select and call methods in the context class. InStrategyThis restriction and intent does not exist in the mode,StrategyThe derived class does not support references to the context class, and does not need to call the method of the context class.
StateThe mode completely separates the logic and action of the state machine. The action is inContextClass, while the logic is distributed inStateClass. This makes it easy for the two to change independently without affecting each other. Another advantage of this method is that it is very efficient, basically nestedSwitch/caseThe efficiency is the same. Therefore, the table-driven method is flexible and nested.Switch/caseMethod efficiency.
The cost of using this mode is reflected in two aspects: first,StateWriting a derived class is boring.20Several state machines are troublesome. Second, the logic is scattered and the entire state logic cannot be seen in one place. ThereforeCodeDifficult to maintain.