【发布时间】:2020-08-04 11:03:46
【问题描述】:
情况:我的状态有一个 entry() 和 exit() 方法,每次状态转换时都需要调用它们。为了确保这一点,我在 State 中使用了一个 changeState() 方法,其中包含必要的过程。上下文每次使用涉及状态机的操作时都会调用它。然而,问题是我每次添加新方法时都需要调用 state.changeState() ,并且我确信有一种方法可以避免代码重复。以下是进一步说明的代码
class Context {
State state;
void method1() {
state.method1();
state.changeState();
}
void method2() {
state.method2();
state.changeState(); // Code duplication!!
}
abstract class State {
Context context;
State next;
void changeState() {
this.exit();
next.entry();
context.setState(next);
}
// other necessary methods
}
class ConcreteState extends State {
void method1() {
// do something
next = AnotherConcreteState;
}
void entry() {/*do something */};
void exit() {/*do something */};
}
如果我想在 Context 中添加额外的方法,如何避免在新方法中每次调用 state.changeState() 的代码重复?
【问题讨论】:
标签: java design-patterns code-duplication state-pattern