如何以OOP方式编写main()?
当我第一次开始编程时,我将所有内容都写在 main 中。 但据我了解,我尝试在 main()
方法中尽可能少地执行操作。
但是您在哪里决定让其他类/方法负责从 main()
接管程序呢? 你怎么做呢?
我见过很多方法,比如:
class Main
{
public static void main(String[] args)
{
new Main();
}
}
还有一些:
class Main {
public static void main(String[] args) {
GetOpt.parse(args);
// Decide what to do based on the arguments passed
Database.initialize();
MyAwesomeLogicManager.initialize();
// And main waits for all others to end or shutdown signal to kill all threads.
}
}
在 main()
中应该做什么和不应该做什么? 还是没有灵丹妙药?
谢谢你的时间!
When I first started programming, I wrote everything in main. But as I learned, I tried to do as little as possible in my main()
methods.
But where do you decide to give the other Class/Method the responsibility to take over the program from main()
? How do you do it?
I've seen many ways of doing it, like this:
class Main
{
public static void main(String[] args)
{
new Main();
}
}
and some like:
class Main {
public static void main(String[] args) {
GetOpt.parse(args);
// Decide what to do based on the arguments passed
Database.initialize();
MyAwesomeLogicManager.initialize();
// And main waits for all others to end or shutdown signal to kill all threads.
}
}
What should and should not be done in main()
? Or are there no silver bullets?
Thanks for the time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
看,“主要”方法的内容和形式非常依赖于语言和环境。 在 Java 中,每个类都可以有一个 public static void main() 方法,因此拥有多个方法是完全可行的。
但现在,让我们通过帕纳斯模块化定律来思考这个问题:“每个模块都隐藏着一个秘密,而这个秘密是可以改变的。” 最初调用的模块的“秘密”是进程与操作系统接口的细节:诸如获取参数和处理不规则终止之类的事情。 在 Python 中,这会导致这样的结果:
这里的重点是,你可以从环境中获取所有内容,然后调用 main() 函数; 您捕获所有未捕获的异常,并在程序终止之前对它们执行一些智能操作。
Look, the content and form of the "main" method is very language and environment dependent. In Java, every class can have a
public static void main()
method, so it's entirely feasible to have more than one.But now, let's think about this via parnas's law of modularization: "every module conceals a secret, and that secret is something that can change." The "secret" of the module that is called initially is the details of interfacing the process with the operating system: things like getting the arguments and handling irregular terminations. In Python, this leads to something like this:
The point here being that you get everything from the environment you can, then call the main() function; you catch all uncaught exceptions and you do something intelligent with them before the program dies.
我认为主要方法应该解释程序在启动时做什么。
因此它可能会调用初始化方法,但逻辑应该提取到方法中。
在您的示例中,我不会创建 Main() 方法,而是将其放入原始方法中。
I think the main method should explain, what the program does at starting up.
So it may call initialzing methods, but the logic should be extracted into methods.
In your example, I would not create a Main() method, but put it into the original one.
你的程序的设计将决定你的“主要”的形状。
恕我直言,有一个“规则”来说明你的主要功能应该如何,这是毫无意义的。
The design of your program will decide the shape of your "main".
Having a "rule" that says how your main function should be, is - IMHO - a non-sense.
请记住,如果有人想了解您的程序如何工作,他们首先会看的地方可能是 main (至少我会)。 所以,我认为投入越少越好并不是一个好主意。 但我想说的是,为了鸟瞰你的程序是如何工作的,要尽可能少地写。
因此,我认为在实现主函数时您最关心的一个问题应该是可读性。
Remember that if someone wants to get an idea of how your program works, the first place they'll probably look is in main (at least I would). So, I don't think it's a good idea to put as little in it as possible. But I would say to put as little as is necessary to get a birds' eye view of how your program works.
Thus, I think your single biggest concern in implementing a main function should be readability.
主函数中的代码:
因此,主函数中的代码:
实际上,这意味着真正的应用程序在 main 中没有太多内容。 玩具应用程序和一次性程序可能在 main 中包含相当多的内容,因为无论如何您都不打算测试或重用它们。
实际上,我上面所说的一些内容是特定于 C++ 的。 当然,Java 主要方法可以由测试代码或变体应用程序调用。 但它们仍然不将对象作为参数,仅将命令行参数作为参数,因此它们在测试下的隔离程度或在重用方面表现良好的程度相当低。 我想您可以传递类名供它们实例化并用于创建应用程序的其余部分。
[编辑:有人从这个问题中删除了“C++,Java”标签。 所以:我上面说的是 C++ 和 Java 特定的。 其他语言可能会以不太特殊的方式对待 main,在这种情况下,您可能也没有特殊的理由要特别对待它。]
Code in the main function:
Therefore code in the main function:
In practice, this means that real apps don't have much in main. Toy apps and one-shot programs might have quite a lot in main, because you aren't planning to test or reuse them anyway.
Actually, some of what I say above is C++-specific. Java main methods of course can be called by test code or variant apps. But they still don't take objects as parameters, only command-line arguments, so the extent to which they can be isolated under test, or behave well in terms of re-use, is quite low. I guess you could pass class names for them to instantiate and use to create the rest of the app.
[Edit: someone has removed the "C++, Java" tags from this question. So: what I say above is C++ and Java specific. Other languages may treat main in a way which is less special, in which case there may be no particular reason for you to treat it specially either.]
在我看来,一个相当大的项目的“主要”应该包含大约 3 个函数调用:
任何大型应用程序都会被分为功能块,通常具有某种层次结构。 主控制器可能有多个用于特定功能的子控制器。
这样做可以更轻松地定位特定功能,并且关注点分离效果更好。
当然,正如其他回复所说,软件开发确实没有灵丹妙药。 对于一个简短的项目,我可能会将所有内容都放在 main 中,以便快速启动并运行。 我认为也取决于语言 - 在特定语言中,某些选项可能比其他选项更容易。
In my opinion, the "main" of a sizable project should contain around 3 function calls:
Any sizable application will be divided into chunks of functionality, usually with some hierarchy. The main controller may have several child controllers for specific features.
Doing it this way makes it much easier to locate specific functionality, and separation-of-concerns is better.
Of course, as other replies have said, there really is no silver bullet in software development. For a short project I might put everything in main just to get things up-and-running quickly. I think also depends on the language - some options may be easier than others in particular languages.
正如他们所说,无论什么都能让你的船漂浮。 :) 你应该真正专注于使代码简单、易于阅读和高效,使用任何你需要的工具来实现这一点。 如果需要在 main 中放置大量代码 - 就这样做。 如果你觉得物体会让事情变得更有条理——那就这么做吧。
创建
class Main
的单个实例,然后调用执行所有工作的实例方法Main()
与直接在 main 方法中编写所有内容一样好。Whatever floats your boat, as they say. :) You should really focus on making the code simple, easy to read and efficient, using whatever tools you need to achieve this. If it warrants putting lots of code in main - do so. If you feel that objects would make things more organized - go that way.
Making a single instance of
class Main
and then calling the instance methodMain()
which does all the work is just as good as writing everything in the main method directly.我想说的是,这不是你的主要功能中的内容,而是不包含的内容。 根据项目的复杂性,您需要将其分解为功能部分,例如“数据库功能”、“显示功能”、“与牧师共进下午茶”等。
这一切都与代码的可读性有关。 以前从未见过您的程序的其他人能否在遇到该程序时,首先对其功能有一个很好的概括性了解?
那么可以很容易地看到哪里可以更深入地挖掘该机制吗?
您使用的每个功能部分是否仅执行流程的逻辑块? 它不必只做一件事,但它不应该做所有加上厨房水槽。
以可由外部源维护的方式分解您的代码。
因为天知道,归根结底,如果其他人可以修复错误,那就更好了 =)
作为对你问题的直接回答,我会将函数调用放在 main 中的每个主要组件中、设置、过程和完成,以便任何查看它的人都可以快速了解程序的工作原理。 如果需要的话,他们可以进一步深入。
I would say that it's not what's in your main function, but what's not. Depending on the complexity of your project, you'll want to break it down into functional sections, like "Database functions", "Display functions", "High Tea with the Vicar", etc.
It's all about readability of code. Can someone else, who has never seen your program before come across it, and get at first, a good generalised idea of what it does?
Can then then easily see where to go to dig a little deeper into the mechanism?
Is each functional section you use doing only a logical block of processes? It doesn't have to only do one thing, but it shouldn't be doing everything plus the kitchen sink.
Break down your code in a way that it's maintainable by an outside source.
Cause heavens knows, when it comes right down to it, if someone -else- can fix the bug, it's all the better =)
As a direct answer to your question, I would put the function calls to each of the major components in main, the setup, the process and the finish, so that anyone who looks at it gets a quick overview of how the program works. They can then drill down further should they need to.