Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - How many classes is too many? When is it appropriate to add new classes? [closed]

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.

like image 759
John Gowers Avatar asked Jul 29 '13 14:07

John Gowers


People also ask

How many classes are too many in Java?

I would say more than 10,000 classes is a lot. This is more than the entire JDK. Keep in mind that often you'll want to use a common superclass with several subclasses, with most of the logic in the superclass and only the stuff that's necessarily different in the subclasses.

How many is too many classes?

Taking 12-15 credits is considered “full-time” in college lingo. That amounts to 4-5 classes, and for young students, that course load is really heavy (let's be honest, it's heavy for MOST students of any age).

How many classes should a Java program have?

But each Java program should have one class declared as public to make it accessible for classes in a different package. Other classes in the same program cannot be public. The name of the class should be the same as the name of the Java file.

How many classes are allowed in Java?

There are 5,000 or so classes built-in to Java, and programmers have written hundreds of thousands if not millions of their own.


1 Answers

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.

like image 166
eis Avatar answered Oct 06 '22 23:10

eis