🔍目的
止步模式用于防止对象在不完整或不合适的状态下执行某些代码。
🔍解释
真实世界例子
洗衣机中有一个开始按钮,用于启动衣物洗涤。当洗衣机处于非活动状态时,按钮将按预期工作,但是如果已经在洗涤,则按钮将不起任何作用。
通俗描述
使用止步模式,仅当对象处于特定状态时才执行特定代码。
维基百科
禁止模式是一种软件设计模式,仅当对象处于特定状态时才对对象执行操作。例如,一个对象读取zip压缩文件并在压缩文件没打开的时候调用get方法,对象将在请求的时候”止步“。
程序示例
洗衣机是一个具有两个状态的对象,可以处于两种状态:ENABLED和WASHING。 如果机器已启用,则使用线程安全方法将状态更改为WASHING。 另一方面,如果已经进行了清洗并且任何其他线程执行wash()
,则它将不执行该操作,而是不执行任何操作而返回。
创建洗衣机类相关部分
@Slf4j
public class WashingMachine {private final DelayProvider delayProvider;private WashingMachineState washingMachineState;public WashingMachine(DelayProvider delayProvider) {this.delayProvider = delayProvider;this.washingMachineState = WashingMachineState.ENABLED;}public WashingMachineState getWashingMachineState() {return washingMachineState;}public void wash() {synchronized (this) {var machineState = getWashingMachineState();LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), machineState);if (this.washingMachineState == WashingMachineState.WASHING) {LOGGER.error("Cannot wash if the machine has been already washing!");return;}this.washingMachineState = WashingMachineState.WASHING;}LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);}public synchronized void endOfWashing() {washingMachineState = WashingMachineState.ENABLED;LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());}
}
这里是一个WashingMachine
所使用的DelayProvider
简单接口。
public interface DelayProvider {void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
}
使用WashingMachine
public static void main(String... args) {final var washingMachine = new WashingMachine();var executorService = Executors.newFixedThreadPool(3);for (int i = 0; i < 3; i++) {executorService.execute(washingMachine::wash);}executorService.shutdown();try {executorService.awaitTermination(10, TimeUnit.SECONDS);} catch (InterruptedException ie) {LOGGER.error("ERROR: Waiting on executor service shutdown!");Thread.currentThread().interrupt();}}
程序的输出。
14:02:52.268 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-2: Actual machine state: ENABLED
14:02:52.272 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-2: Doing the washing
14:02:52.272 [pool-1-thread-3] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-3: Actual machine state: WASHING
14:02:52.273 [pool-1-thread-3] ERROR com.iluwatar.balking.WashingMachine - Cannot wash if the machine has been already washing!
14:02:52.273 [pool-1-thread-1] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-1: Actual machine state: WASHING
14:02:52.273 [pool-1-thread-1] ERROR com.iluwatar.balking.WashingMachine - Cannot wash if the machine has been already washing!
14:02:52.324 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - 14: Washing completed.
🔍类图
Balking
🔍适用场景
使用止步模式场景
- 您只想在对象处于特定状态时才对其调用操作
- 对象通常仅处于容易暂时停止但状态未知的状态