I have a question about how to structure code in relation to GUI objects. Suppose I have a dialog that has a list control that has a bunch of names obtained from a database. The user can edit the names. Does the logic reside inside that dialog class or should it be from the outside. To illustrate what I mean, here’s some pseudo code showing the structure of the code when the logic is handled outside the dialog class:
NamesDialog : wxDialog
{
Private:
..stuff..
Public:
...
SetNames(wxStringArray names);
wxStringArray GetNames();
..stuff..
}
So the user of the class would do something like:
wxStringArray names = DatabaseManager::Get()->GetNames();
names.Sort();
NamesDialogObject.SetNames(names);
NamesDialogObject.ShowModal();
wxStringArray modified_names = NamesDialogObject.GetNames();
AddToDatabase(modified_names); //or something like this.
On the other hand, the database logic can reside inside the NamesDialog class itself. In the show method I can query the database for the names and as the user interacts with the controls (list control in this case), the database can be updated from the event handlers. As a result the NamesDialog class only has the Show() method as there is no need to use SetNames or GetNames() etc.
Which method is generally preferred? I don’t have much work experience so I’m not sure which is the proper way to handle it. Sometimes it's easier to handle everything in the class but getting access to objects it interacts with can be challenging. Generally can do it by having the relevant objects be singletons like the database manager in the above example.
Try MVC on for size.
There are few similar approaches when designing a gui program :
Ok, the last one is a joke.
The first two are preferred ways of designing a gui program. Both are good, so whichever you pick you won't make a mistake. Then you should fully unit test your logic, which should be in presenter and model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With