Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the program logic reside inside the gui object class or be external to the class?

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.

like image 763
hd112 Avatar asked Feb 21 '11 23:02

hd112


2 Answers

Try MVC on for size.

like image 128
Lightness Races in Orbit Avatar answered Sep 24 '22 06:09

Lightness Races in Orbit


There are few similar approaches when designing a gui program :

  • presenter first
  • MVP
  • cowboy coding

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.

like image 33
BЈовић Avatar answered Sep 25 '22 06:09

BЈовић