-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8d8a4a
commit 476ef4d
Showing
5 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# 状态模式 | ||
状态模式`State Pattern`是一种对象行为型模式,其以面向对象的方式实现状态机,使用状态模式,通过将每个单独的状态实现为状态模式接口的派生类,并通过调用由模式的超类定义的方法来实现状态转换,来实现状态机,状态模式也可以解释为策略模式,它能够通过调用模式接口中定义的方法来切换当前策略。 | ||
|
||
## 描述 | ||
在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的`stateful`对象,此种对象状态是从事先定义好的一系列值中取出的,当一个这样的对象与外部事件产生互动时,其内部状态就会改变,从而使得系统的行为也随之发生变化,状态模式允许一个对象在其内部状态改变时改变其行为,通常用于解决过多的`if else`条件分支问题。 | ||
|
||
### 优点 | ||
* 封装了转换规则。 | ||
* 枚举可能的状态,在枚举状态之前需要确定状态种类。 | ||
* 将所有与某个状态有关的行为放到一个类中,并且可以方便地增加新的状态,只需要改变对象状态即可改变对象的行为。 | ||
* 允许状态转换逻辑与状态对象合成一体,而不是某一个巨大的条件语句块。 | ||
* 可以让多个环境对象共享一个状态对象,从而减少系统中对象的个数。 | ||
|
||
### 缺点 | ||
* 状态模式的使用必然会增加系统类和对象的个数。 | ||
* 状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱。 | ||
* 状态模式对“开闭原则”的支持并不太好,对于可以切换状态的状态模式,增加新的状态类需要修改那些负责状态转换的源代码,否则无法切换到新增状态;而且修改某个状态类的行为也需修改对应类的源代码。 | ||
|
||
|
||
### 适用环境 | ||
* 对象的行为依赖于它的状态(属性)并且可以根据它的状态改变而改变它的相关行为。 | ||
* 代码中包含大量与对象状态有关的条件语句,这些条件语句的出现,会导致代码的可维护性和灵活性变差,不能方便地增加和删除状态,使客户类与类库之间的耦合增强。在这些条件语句中包含了对象的行为,而且这些条件对应于对象的各种状态。 | ||
|
||
|
||
## 实现 | ||
|
||
```javascript | ||
// 例子:我们能够改变输入的文本的状态 | ||
// 如果选择大写,将开始以大写打印 | ||
// 如果选择小写,那么就用小写打印等等 | ||
|
||
const upperCase = str => str.toUpperCase(); | ||
const lowerCase = str => str.toLowerCase(); | ||
const defaultTransform = str => str; | ||
|
||
class TextEditor { | ||
constructor(transform) { | ||
this._transform = transform; | ||
} | ||
|
||
setTransform(transform) { | ||
this._transform = transform; | ||
} | ||
|
||
type(words) { | ||
console.log(this._transform(words)); | ||
} | ||
} | ||
|
||
(function(){ | ||
const editor = new TextEditor(defaultTransform); | ||
|
||
editor.type("First line"); // First line | ||
|
||
editor.setTransform(upperCase); | ||
|
||
editor.type("Second line"); // SECOND LINE | ||
editor.type("Third line"); // THIRD LINE | ||
|
||
editor.setTransform(lowerCase); | ||
|
||
editor.type("Fourth line"); // fourth line | ||
editor.type("Fifth line"); // fifth line | ||
})(); | ||
``` | ||
|
||
|
||
## 每日一题 | ||
|
||
``` | ||
https://github.com/WindrunnerMax/EveryDay | ||
``` | ||
|
||
## 参考 | ||
|
||
``` | ||
https://www.runoob.com/design-pattern/state-pattern.html | ||
https://github.com/sohamkamani/javascript-design-patterns-for-humans#-state | ||
https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/state.html | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters