7

This is intended as a sort of general question, but I'll give you my specific problem first:

I'm writing a GUI for my program at the moment for a program that will take the form of a frame with various widgets (labels, text fields etc.) placed on it. It's initially going to use the javax.swing library, but I'm laving a layer of abstraction between the library and the GUI itself to make it easier to create a web application that does the same thing.

I'd like to have one class that represents the layout of the GUI: that is to say, it should contain all the information about where the various labels, buttons etc. are placed on the content pane. At the moment, the implementation I'm considering is the following:

class Layout
{
  public MyLabel titleLabel;
  public myTextField sequenceTextField;
  [...]

  public void drawTitleLabel()
  {
    titleLabel = new MyLabel("This is the title.");
    titleLabel.setLocation(wherever);
    titleLabel.draw();
  }

  public void drawSequenceTextField()
  {
    sequenceTextField = new MyTextField();
    [...]
    sequenceTextField.draw();
  }

  [...]

  public void drawGuiFrame()
  {
    drawTitleLabel();
    drawSequenceTextField();
    [...]
  }
}

So I declare each widget on the content pane as a field of the class Layout, and then create methods drawWidgetName that draw each widget. Lastly, I create a method that calls each of the draw... methods in order to draw the whole GUI.

This seems wrong, though. The draw... methods feel as if they should be methods of the individual widgets themselves, not of the larg-scale layout of the entire GUI. This suggests that I should create separate classes TitleLabel, SequenceTextField etc., each with a draw method, and then just have a drawGuiFrame method in the Layout class that calls all of these draw methods (I could even create an abstract class that these new classes could extend).

There would be a few other immediate advantages to this. Suppose I wanted to incorporate a system of checkboxes as well as my labels and text areas. Then I could declare that as its own class and give it properties isOptionA, isOptionB etc. to record which checkbox was ticked.

But I am slightly reluctant to implement the GUI this way, for two reasons. Firstly, creating so many new classes would clutter up the codebase with lots of small .java files. Secondly, these would be very much 'single use' classes: I would never use the code again, since the classes would be designed purely for this particular GUI.

So - how many classes is too many? Is it OK to create lots of new classes that are only going to be used once just because you feel they should be classes? As I say, although I welcome advice specific to this particular problem, I'm also looking for more general pointers about when it's appropriate to add new classes.

John Gowers
  • 2,646
  • 2
  • 24
  • 37

2 Answers2

10

Having separate classes seems correct approach to me. You organize the classes in packages, modules and submodules so they really should not "clutter the codebase". A large application in java can easily have lots and lots of classes which might be used only by that single application. That is pretty much the way java as a language and java ecosystem as a whole works.

In java, it is usually considered that the more granular class layout you have, the better.

eis
  • 51,991
  • 13
  • 150
  • 199
  • Thanks for your answer. Would it be appropriate to nest the classes within a larger class if they're only used once, or might that make the class too big? – John Gowers Jul 31 '13 at 12:08
  • @Donkey_2009 depends. In some use cases it is appropriate. – eis Nov 15 '13 at 07:01
3

I myself like to create a class for each new introduced concept. Usually a class can be arbitrarily long, especially if you use a lot of hardcoded data in it. You will have to watch out for god objects http://en.wikipedia.org/wiki/God_object.

If some class has too much logic in it, it becomes a lot harder to read. If you feel like a class is getting too large, see if there is logic in the class that could have its own class.

bas
  • 1,678
  • 12
  • 23