Skip to content

Commit a471ce2

Browse files
Al-assadohbus
andauthored
translated the balking pattern into Chinese (iluwatar#1727)
Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
1 parent 74caa0c commit a471ce2

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

zh/balking/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
layout: pattern
3+
title: Balking
4+
folder: balking
5+
permalink: /patterns/balking/
6+
categories: Concurrency
7+
tags:
8+
- Decoupling
9+
---
10+
11+
## 作用
12+
13+
阻止模式用于防止一个对象在不完整或不适当的状态下执行某段代码。
14+
15+
## 解释
16+
17+
真实世界的案例
18+
19+
> 洗衣机里有一个用于启动衣物洗涤的启动按钮。当洗衣机没有启动时,该按钮可以正常按下生效,但如果洗衣机已经在洗衣服了,再按下按钮就不生效了。
20+
21+
简而言之
22+
23+
> 使用阻止模式,只有当对象处于特定状态时,才会执行某段代码。
24+
25+
维基百科的解释
26+
27+
> 阻止模式是一种软件设计模式,它只在对象处于特定状态时对其执行动作。例如,如果一个对象读取 ZIP 文件,当 ZIP 文件没有打开时,如果一个方法在该对象上调用一个获取方法,该对象就会对阻止这个请求。
28+
29+
**编程示例**
30+
31+
在这个例子的实现中,`WashingMachine` 对象存在 2 种状态: `ENABLED``WASHING`。如果该对象处于 `ENABLED` 状态,则使用一个线程安全的方法可以其状态改变为 `WASHING`。在另一方面,如果它已经处于 `WASHING` 状态,而任何其他线程执行了 `wash()`,它不会执行该指令,而是什么都不做就返回。
32+
33+
以下是 `WashingMachine` 类的相关代码。
34+
35+
```java
36+
@Slf4j
37+
public class WashingMachine {
38+
39+
private final DelayProvider delayProvider;
40+
private WashingMachineState washingMachineState;
41+
42+
public WashingMachine(DelayProvider delayProvider) {
43+
this.delayProvider = delayProvider;
44+
this.washingMachineState = WashingMachineState.ENABLED;
45+
}
46+
47+
public WashingMachineState getWashingMachineState() {
48+
return washingMachineState;
49+
}
50+
51+
public void wash() {
52+
synchronized (this) {
53+
var machineState = getWashingMachineState();
54+
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), machineState);
55+
if (this.washingMachineState == WashingMachineState.WASHING) {
56+
LOGGER.error("Cannot wash if the machine has been already washing!");
57+
return;
58+
}
59+
this.washingMachineState = WashingMachineState.WASHING;
60+
}
61+
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
62+
this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);
63+
}
64+
65+
public synchronized void endOfWashing() {
66+
washingMachineState = WashingMachineState.ENABLED;
67+
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
68+
}
69+
}
70+
```
71+
72+
以下是 `WashingMachine` 使用的简单 `DelayProvider` 接口。
73+
74+
```java
75+
public interface DelayProvider {
76+
void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
77+
}
78+
```
79+
80+
现在我们介绍一下使用 `WashingMachine` 的应用。
81+
82+
```java
83+
public static void main(String... args) {
84+
final var washingMachine = new WashingMachine();
85+
var executorService = Executors.newFixedThreadPool(3);
86+
for (int i = 0; i < 3; i++) {
87+
executorService.execute(washingMachine::wash);
88+
}
89+
executorService.shutdown();
90+
try {
91+
executorService.awaitTermination(10, TimeUnit.SECONDS);
92+
} catch (InterruptedException ie) {
93+
LOGGER.error("ERROR: Waiting on executor service shutdown!");
94+
Thread.currentThread().interrupt();
95+
}
96+
}
97+
```
98+
99+
以下是程序的控制台输出。
100+
101+
```
102+
14:02:52.268 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-2: Actual machine state: ENABLED
103+
14:02:52.272 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-2: Doing the washing
104+
14:02:52.272 [pool-1-thread-3] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-3: Actual machine state: WASHING
105+
14:02:52.273 [pool-1-thread-3] ERROR com.iluwatar.balking.WashingMachine - Cannot wash if the machine has been already washing!
106+
14:02:52.273 [pool-1-thread-1] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-1: Actual machine state: WASHING
107+
14:02:52.273 [pool-1-thread-1] ERROR com.iluwatar.balking.WashingMachine - Cannot wash if the machine has been already washing!
108+
14:02:52.324 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - 14: Washing completed.
109+
```
110+
111+
## 类图
112+
113+
![alt text](../../balking/etc/balking.png "Balking")
114+
115+
## 适用性
116+
117+
在以下情况下可以使用阻止模式:
118+
119+
* 你想要在某个对象上调用一个动作,只有当该对象处于特定状态时才允许该调用。
120+
* 对象一般只处于容易暂时阻止的状态,只不过该时间是未知的。
121+
122+
## 教学
123+
124+
* [Guarded Suspension Pattern](https://java-design-patterns.com/patterns/guarded-suspension/)
125+
* [Double Checked Locking Pattern](https://java-design-patterns.com/patterns/double-checked-locking/)
126+
127+
## 鸣谢
128+
129+
* [Patterns in Java: A Catalog of Reusable Design Patterns Illustrated with UML, 2nd Edition, Volume 1](https://www.amazon.com/gp/product/0471227293/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0471227293&linkId=0e39a59ffaab93fb476036fecb637b99)

0 commit comments

Comments
 (0)