Subject area - an automated workplace with visualization of technological process.
Imagine that there are several identical devices that have two states: open / closed. This is represented by boolean variables:
bool firstDeviceOpened = x; bool secondDeviceOpened = y; There is an enum described like this:
enum State { STATE_NOTHING_OPENED, STATE_FIRST_OPENED, STATE_SECOND_OPENED, STATE_ALL_OPENED }; States can be the following (0 and 1 - "closed" and "open", respectively):
device1 | device2 | method ----------------------------------------- 0 | 0 | STATE_NOTHING_OPENED 1 | 0 | STATE_FIRST_OPENED 0 | 1 | STATE_SECOND_OPENED 1 | 1 | STATE_ALL_OPENED Having all this, it is necessary to initialize the state variable with the current state. In the project, these initializations are implemented like this:
State currentState = STATE_NOTHING_OPENED; if (firstDeviceOpened && !secondDeviceOpened) { currentState = STATE_FIRST_OPENED; } if (!firstDeviceOpened && secondDeviceOpened) { currentState = STATE_SECOND_OPENED; } if (firstDeviceOpened && secondDeviceOpened) { currentState = STATE_ALL_OPENED; } ... // где-то дальше сидит switch, который по состоянию запускает // методы, отображающие смену состояния на визуализации Actually the problem is that when adding another device you need to fence a lot of conditions. As a result, it is somehow sad to support 2 N conditions, where N is the number of devices.
Another stick in the wheels puts the situation when there are devices that work in " special cases ", for example:
// если оба закрыты, открывается третье устройство bool thirdDeviceOpened = !(firstDeviceOpened || secondDeviceOpened); How to get rid of the need to fence a bunch of if and handle states for each separate combination?
if? take and see the value from the table for one action - VTT