What design pattern do I use to synchronize state changes of the object model with the state of the GUI?

I usually work on graphical interfaces where I want the user to not be able to do what they could not do by disabling the controls on the form. For example, maybe I don’t want the "Process Widgets" button to be enabled if the user does not have a "widget project" open in the application? Or the “Cancel processing” button is activated only when the widget starts.

Is there a commonly used design pattern for synchronizing state changes in an object model with GUI state?

+4
source share
4 answers

It looks like you are looking for the opportunity to subscribe to certain events. I would say using an observer pattern. The state template is a bit clogged for something in this situation [if it was more than a binary condition, then you could customize the state template to work on it]

+1
source

The MVC and Command templates should work here. The main idea is that you need to send a “message” (which may be an object or a string or something really) in the GUI to notify about it, to change its state and how to do it. You just have to be careful to take into account any delays that may occur when receiving and processing a GUI message.

You can create a queue of events (associated with commands) and make sure that the first of them is the first (because of the queue). Then follow the GUI process of the next event in the queue, this will ensure that the GUI is updated before starting another event to do what you were just trying to disable. You can then report the error or ignore it or something else. In any case, the user will not be able to do something as soon as the team disables this functionality. Disabling the GUI button is just a visual effect, and for commands in the background there should be a warning code that really does your job.

+1
source

I am not an expert, but recently I read about the Model-View-ViewModel template for WPF, which is basically an MVC template that uses WPF features. Since you are on winforms, you can try the Model-View-Presenter template. There is an article about it here .

If you can switch to WPF (although I have a feeling that winforms has something similar), it looks like you definitely want to use the Commands function. It has some features that you want to embed. In principle, any button or something else subscribes to a command, and there is one logical part that determines whether the command is enabled or disabled. You just need to do this to check the state of the model.

0
source

This does not apply to winforms, but Qt recently introduced a state-machine structure to facilitate changing the state of user interface elements based on the internal state of the application. See the Qt documentation for more information.

This idea can probably be applied to a winforms-based application. However, this may be worth a lot of initial effort. I do not know any existing implementations.

0
source

Source: https://habr.com/ru/post/1301071/


All Articles