Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
wchaowu committed Aug 16, 2013
1 parent ba6a577 commit 8ad5830
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions JavaScript-Design-Patterns/State-Pattern/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
隆有走动,攻击,防御,跌倒,跳跃等等多种状态,而这些状态之间既有联系又互相约束。<br />
比如跳跃的时候是不能攻击和防御的。跌倒的时候既不能攻击又不能防御,而走动的时候既可以攻击也可以跳跃。<br />
要完成这样一系列逻辑, 常理下if else是少不了的. 而且数量无法估计, 特别是增加一种新状态的时候, 可能要从代码的第10行一直改到900行. <br />
'''javascript


```javascript
if ( state === 'jump' ){
if ( currState === 'attack' || currState === 'defense' ){
return false;
Expand All @@ -15,8 +17,11 @@ if ( state === 'jump' ){
return true;
}
}

```

为了消灭这些if else, 并且方便修改和维护, 我们引入一个状态类.

```javascript
var StateManager = function(){
var currState = 'wait';
Expand Down Expand Up @@ -46,4 +51,5 @@ var StateManager = function(){
}
var stateManager = StateManager();
stateManager.changeState( 'defense' );
```
```
Edit By [MaHua](http://mahua.jser.me)

0 comments on commit 8ad5830

Please sign in to comment.