java-多少个类太多?什么时候添加新的类合适? [英] java - How many classes is too many? When is it appropriate to add new classes?

查看:68
本文介绍了java-多少个类太多?什么时候添加新的类合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种一般性问题,但我先向您介绍我的具体问题:

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

我现在正在为程序编写GUI,该程序将采用框架的形式,并带有各种小部件(标签,文本字段等).最初将使用javax.swing库,但是我在库和GUI本身之间添加了一层抽象,以简化创建具有相同功能的Web应用程序的过程.

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.

我想拥有一个代表GUI布局的类:也就是说,它应该包含有关各种标签,按钮等在内容窗格上放置位置的所有信息.目前,我正在考虑的实现如下:

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();
    [...]
  }
}

因此,我在内容窗格上将每个窗口小部件声明为类Layout的字段,然后创建绘制每个窗口小部件的方法drawWidgetName.最后,我创建了一个调用每个draw...方法的方法,以绘制整个GUI.

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.

但是,这似乎是错误的. draw...方法看起来好像它们应该是各个小部件本身的方法,而不是整个GUI的较大比例的布局.这表明我应该创建单独的类TitleLabelSequenceTextField等,每个类都有一个draw方法,然后在Layout类中只有一个drawGuiFrame方法,该类将调用所有这些draw方法(我什至可以创建一个抽象类,这些新类可以扩展).

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).

这将有其他一些直接的优势.假设我要合并一个复选框系统以及我的标签和文本区域.然后,我可以将其声明为自己的类,并为其提供属性isOptionAisOptionB等,以记录选中了哪个复选框.

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.

但是出于两个原因,我有点不愿以这种方式实现GUI.首先,创建这么多新类会使许多小的.java文件杂乱无章.其次,这些将是非常单一用途"的类:我将不再使用代码,因为这些类将纯粹是为特定的GUI设计的.

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.

推荐答案

对我来说,拥有单独的类似乎是正确的方法.您将类组织为 packages modules submodules ,因此它们实际上不应使代码库杂乱无章". Java中的大型应用程序可以轻松地拥有很多类,而这些类只能由该单个应用程序使用.这几乎就是Java作为一种语言和整个Java生态系统的工作方式.

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.

在Java中,通常认为类布局越细越好.

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

这篇关于java-多少个类太多?什么时候添加新的类合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆