Java - how much logic to put in the main class?

后端 未结 4 2173
暗喜
暗喜 2021-02-20 18:36

How much logic do you normally put in the main class? Should logic in the main class be at minimum, only instantiating other, specialized classes, and running all the tasks from

相关标签:
4条回答
  • 2021-02-20 19:15

    It's not so much a question of whether a class is "the main class". It's a question of how much logic is in the public static void main(String args[]) method. Ideally, it should contain very little logic. It should essentially construct one or two objects, and then call methods on those objects. One of those objects might be a this() - an instance of the main class, and that's ok.

    You have to put the main() method somewhere - there's no need to create a special class just to hold that method.

    As a general rule, try to avoid having too much in static methods - static methods can't be mocked for testing.

    0 讨论(0)
  • 2021-02-20 19:18

    Should logic in the main class be at minimum, only instantiating other, specialized classes, and running all the tasks from there?

    Yes. main method and its surrounding class should ideally be used only as an entry point to start the program. The mere existence of the surrounding class is just an artifact of the way Java programs are composed (everything must be inside some class), and there's no reason why it should contain other stuff in addition to the main method (but there definitely are reasons why it shouldn't).

    When you get the interesting classes (those that form the actual program) separated, you open doors for all kinds of flexibility. Perhaps some of those classes could be used in some other projects. Perhaps some day you'll want to replace some of them with better implementations. Maybe you'll find a better order to instantiate all those classes - so just swap a few lines. Or how about executing lengthy startup loadings and instantiations in parallel threads? Just wrap some of them to suitable Executors. Good luck trying this with a 1000+ line main class.

    This kind of flexibility matters for everything except maybe for 100-line elementary examples, prototypes and such. But given that even small tools tend to grow, why not do it correctly right from the beginning?

    0 讨论(0)
  • 2021-02-20 19:23

    For small tools, I'm happy to have most or all of the logic in the main class - there tends to be less of a model to work with. (For very small tools, I confess I usually don't bother with unit tests. In particular, there's less benefit on the design side of things than there is if you're building something which will be a component in a larger app.)

    For large scale apps, the main class is really just involved with setting things up and getting them in motion. If you're using a DI framework that can be very little code indeed; if you're not using dependency injection then the main class often acts as a "manual" dependency injection framework.

    0 讨论(0)
  • 2021-02-20 19:26

    The main class should be an entry point to your program and should thus be relatively small. However this all depends on your actual program. If it's 50 lines long, it might be overkill to create two files for it.

    As an example, consider the default Swing Application Framework Desktop Application as it would be generated by the NetBeans template, which is simple and short, and whose main() method is a single line that launches it:

    public class MyApp extends SingleFrameApplication {
    
        @Override protected void startup() {
            show(new MyView(this));
        }
    
        @Override protected void configureWindow(java.awt.Window root) {}
    
        public static MyApp getApplication() {
            return Application.getInstance(MyApp.class);
        }
    
        public static void main(String[] args) {
            launch(MyApp.class, args);
        }
    }
    
    0 讨论(0)
提交回复
热议问题