Skip to content

Commit

Permalink
✏更新 策略模式
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcaffebabe committed May 31, 2021
1 parent eaa4c0d commit e5a3f84
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Binary file added assets/202153110326.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions 软件工程/设计模式/行为模式.md
Expand Up @@ -157,11 +157,11 @@ class UserBannedState{
- 策略模式与状态模式之间的区别在于状态模式的各个状态之间是有联系的

![](https://design-patterns.readthedocs.io/zh_CN/latest/_images/Strategy.jpg)
![202153110326](/assets/202153110326.jpg)

```java
interface Strategy{
void algorithm();
void execute(..);
}

class Context {
Expand All @@ -172,7 +172,7 @@ class Context {
}

void execute(){
strategy.algorithm();
strategy.execute(..);
}
}

Expand All @@ -184,6 +184,10 @@ Context context = new Context(new StrategyA());
context.execute();
```

策略模式要求客户代码必须了解不同策略模式的差异,也就是客户必须针对具体实现进行编码。

另外一个要考虑的是不同策略所需要的参数不同,虽然可以一股脑将全部参数传递给具体策略,但为了降低性能开销,某些策略并不需要用到那么多参数,此时可以传递一个Context的引用给具体策略,让具体策略按需索取,但这种方式则对Context接口的粒度做了过细的要求。

## 模板方法

> 在父类当中定以算法骨架,将一些步骤延迟到子类当中实现
Expand Down

0 comments on commit e5a3f84

Please sign in to comment.