Should the program logic reside inside the gui object class or be external to the class?
Posted
by
hd112
on Stack Overflow
See other posts from Stack Overflow
or by hd112
Published on 2011-02-21T23:13:06Z
Indexed on
2011/02/21
23:25 UTC
Read the original article
Hit count: 168
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.
© Stack Overflow or respective owner