I recently listened to a technical talk about pure coding. The speaker was a test engineer who emphasized that he avoided "if" statements in the code and made the most of polymorphism. He also opposed global states.
I completely agree with him, but I need to clarify how to replace the global state and the expression “if” using polymorphism for the scenario below,
There are 3 states in my document. I want to change the state of user interface components based on the state of the document. Right now, I'm using "if" blocks and an enum type that hold the current state of the document to go to the states of the user interface components.
eg:
enum DOC_STATE
{
DOC_STATE_A = 0,
DOC_STATE_B,
DOC_STATE_C
};
void QMainWindow::handleUi(_docState)
{
switch(_docState)
{
case (DOC_STATE_A):
{
menu.disable();
....
}
case (DOC_STATE_B):
{
menu.enable();
...
}
case (DOC_STATE_C):
{
...
}
}
I think I can have separate child classes for each state and have a handleUI () method in each class. A call to the handleUi () method calls the correct method call. But I will say that I support these objects in my document, how do I switch from one object to another every time a state transition occurs?
In other words, how to handle the transition of the user interface by monitoring the state of a document without using the global state and the “if” or “Switch statements”?
Qt. .