diff --git a/notes/设计模式.md b/notes/设计模式.md index 1211d4d3..4da4c967 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -1459,9 +1459,9 @@ Context 的 request() 方法委托给 State 对象去处理。当 Context 组合 但是状态模式是通过状态转移来改变 Context 所组合的 State 对象,而策略模式是通过 Context 本身的决策来改变组合的 Strategy 对象。 -所谓的状态转移,是指 Context 在运行过程中由于一些条件发生改变而使得 State 对象发生改变,主要必须要是在运行过程中。 +所谓的状态转移,是指 Context 在运行过程中由于一些条件发生改变而使得 State 对象发生改变,注意必须要是在运行过程中。 -状态模式主要是用来解决状态转移的问题,当状态发生庄毅了,那么 Context 对象就会改变它的行为;而策略模式主要是用来封装一组可以互相替代的算法族,并且可以根据需要动态地去替换 Context 需要使用哪个算法。 +状态模式主要是用来解决状态转移的问题,当状态发生转移了,那么 Context 对象就会改变它的行为;而策略模式主要是用来封装一组可以互相替代的算法族,并且可以根据需要动态地去替换 Context 需要使用哪个算法。 **4. 问题描述** @@ -1481,7 +1481,7 @@ Context 的 request() 方法委托给 State 对象去处理。当 Context 组合 糖果销售机即 Context。 -下面的实现中每个 State 都组合了 Context 对象,这是因为状态转移的操作在 State 对象中,而状态转移过程又必须改变 Context 对象的 state 对象,因此 State 必须拥有 Context 对象。 +下面的实现中每个 State 都组合了 Context 对象,这是因为状态转移的操作在 State 对象中,而状态转移过程又必须改变 Context 对象的 state 对象,因此 State 必须组合 Context 对象。 ```java public interface State { @@ -1508,6 +1508,7 @@ public interface State { ``` ```java public class HasQuarterState implements State{ + private GumballMachine gumballMachine; public HasQuarterState(GumballMachine gumballMachine){ @@ -1539,6 +1540,7 @@ public class HasQuarterState implements State{ ``` ```java public class NoQuarterState implements State { + GumballMachine gumballMachine; public NoQuarterState(GumballMachine gumballMachine) { @@ -1599,6 +1601,7 @@ public class SoldOutState implements State { ``` ```java public class SoldState implements State { + GumballMachine gumballMachine; public SoldState(GumballMachine gumballMachine) { @@ -1634,6 +1637,7 @@ public class SoldState implements State { ``` ```java public class GumballMachine { + private State soldOutState; private State noQuarterState; private State hasQuarterState; @@ -1703,6 +1707,7 @@ public class GumballMachine { ``` ```java public class GumballMachineTestDrive { + public static void main(String[] args) { GumballMachine gumballMachine = new GumballMachine(5); @@ -1782,7 +1787,7 @@ No gumball dispensed 过度使用设计模式可能导致代码被过度工程化,应该总是用最简单的解决方案完成工作,并在真正需要模式的地方才使用它。 -反模式:不好的解决方案来解决一个问题。主要作用是为了警告不要使用这些解决方案。 +反模式:不好的解决方案来解决一个问题。主要作用是为了警告人们不要使用这些解决方案。 模式分类: