Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Structuring a progam: Main class contains data and methods or seper

  Asked By: John    Date: Jun 06    Category: Java    Views: 816
  

Let's say I'm writing a program with a gui that displays a JTable of
data based on a list of objects. I would normally have something like:

Main.java - Loads data files as a static ArrayList of objects, then
creates and displays the main window. Contains methods like getObject
(int index) that the window can call to get the data values. The only
methods contained in Main.java are static methods like 'loadData'
and 'getObject'. The windows have their own class methods for doing
whatever else they need to do. The Main.java class acts as the main
data stor,e rather than having a seperate object that contains the
data.

Window.java - Displays the data. When buttons are clicked it will
often call static methods like Main.getObject(index) for retrieving
data, etc.

EditingWindow.java - displays the details of an individual object.
When buttons are clicked, calls Main.updateDetails() which updates
the object stored in Main.java, and this method also fires an event
to the window's JTable so it will update.

The way it is structured is the Main class acts as a central node
that deals with what goes where. All the other classes have to pass
their data to the Main class in order for it to be sent elsewhere
(e.g. to different windows).

My question is something like... should I have:
(a) As above with the Main.java class doubling as the main data store
with getObject(index) methods.
(b) Have a seperate class called MyObjectList as a static variable in
Main.java, this object represents the list of data, and has all the
get/update methods. Classes like the window can call Main.getList
().getObject(index).
(c) Have a seperate class called MyObjectList as in (b), but the
Main.java class has methods like getObject(index) which simply
says '{ return myObjectList.getObject(index); }'

Also, is there any downside to using static methods? e.g. you could
pass the list of objects to the Window class constructor so it would
have it's own reference to the list. Would this be better or worse?

I realise that this question is something akin to trying to read
someone else's code, and that there are probably loads of different
ways to do it and it might just depend on the size and complexity f
the program or just personal style, but all comments are appreciated.
I would actually be very interested to know how different people
structure their programs.

Share: 

 

No Answers Found. Be the First, To Post Answer.

 




Tagged: