diff --git a/notes/pics/state-gumball.png b/notes/pics/state-gumball.png new file mode 100644 index 00000000..efee450d Binary files /dev/null and b/notes/pics/state-gumball.png differ diff --git a/notes/设计模式 - 状态.md b/notes/设计模式 - 状态.md index 42a30bd8..3fac013b 100644 --- a/notes/设计模式 - 状态.md +++ b/notes/设计模式 - 状态.md @@ -12,7 +12,7 @@ 糖果销售机有多种状态,每种状态下销售机有不同的行为,状态可以发生转移,使得销售机的行为也发生改变。 -

+

```java public interface State { diff --git a/tools/diagram/src/state-gumball.svg b/tools/diagram/src/state-gumball.svg new file mode 100644 index 00000000..c2c2172c --- /dev/null +++ b/tools/diagram/src/state-gumball.svg @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + 糖果机的状态转移 + + NoQuarterState + 没有投币 + + HasQuarterState + 已投币 + + SoldState + 出货中 + + SoldOutState + 售罄 + + insertQuarter() + + ejectQuarter() + + turnCrank() + + dispense(),count > 0 + + dispense(),count == 0 + GumballMachine 把每个操作转交给当前状态对象,状态对象在合适的时候调用 setState 切换状态,避免一大串 if-else。 +