简体   繁体   English

将Java中的“主要”方法及其“辅助”方法分组在一起

[英]Grouping together “main” methods and their “helper” methods in Java

Avoiding large, monolithic methods is considered a good practice. 避免使用大型的整体方法是一种好的做法。 I, personally, like to identify all pieces of code that serve a unique, unambiguous purpose and refactor them into a method. 我个人希望识别出具有唯一,明确目的的所有代码,并将其重构为一种方法。 This way, the code reads more like a book. 这样,代码读起来更像一本书。

The obvious problem with this approach is that my class ends up having a large number of methods available outside their intended scope, which I find highly undesirable. 这种方法的明显问题是,我的类最终在其预期范围之外有大量可用的方法,我认为这是非常不希望的。

There are ways to create nested functions in Java, but since the feature is not directly supported by the language, the resulting code is generally unfathomably ugly --at least, to me. 有多种方法可以用Java创建嵌套函数,但是由于该语言未直接支持该功能,因此,至少对于我来说,生成的代码通常难以理解。

One could also use nested classes. 也可以使用嵌套类。 What I don't like about this solution is that it's somewhat clumsy --is it?-- when some of the involved methods in the "grouping together" are overridden methods. 我不喜欢这种解决方案的是,它有些笨拙-是吗?-当“分组在一起”中的某些涉及方法被重写方法时。

Rather vague question, but anyway I'd like to know how people go about doing this. 这个问题比较模糊,但无论如何,我想知道人们如何去做。

EDIT: Example of what I mean: 编辑:我的意思是示例:

public class ClassWithTwoMainMethods {

    private int var1;
    private int var2;

    public void doSomething(int a) {
        if (conditionToCheck(a)) {
            doSomethingSpecific();
        }
    }

    private void doSomethingSpecific() {
        ...
    }

    private boolean conditionToCheck(int a) {
        ...
    }

    public void doSomethingElse(int a, int b) {
        doSomethingElseHelper1(a+b);
        doSomethingElseHelper2();
        doSomethingElseHelper3();
    }

    private void doSomethingElseHelper1(int arg) {
        ...
    }

    private void doSomethingElseHelper2() {
        ...
    }

    private void doSomethingElseHelper3() {
        ...
    }

}

At first glance, it isn't obvious that the class above has one "main" method with two "helpers" that should not be used anywhere else, and another "main" method with three helpers. 乍一看,上面的类没有一个带有两个不应在其他地方使用的“帮助器”的“ main”方法,以及另一个带有三个帮助器的“ main”方法。

I use "worker objects" for this. 我为此使用“工人对象”。 A worker object exists only inside of a method and helps to achieve a goal. 辅助对象仅存在于方法内部,有助于实现目标。 A typical example for this is String except that this worker is so useful that methods often return it. 一个典型的例子是String除了这个工作程序非常有用,以至于方法经常返回它。

So what I do is I group methods in a worker object, create it in a public API method (ie something is supposed to be used and documented in the public API) and let it do it's thing: 因此,我要做的是将方法分组在一个worker对象中,在一个公共API方法中创建它(即应该在公共API中使用和记录某些东西),然后让它完成它的工作:

public void doSomethingElse(int a, int b) {
    new Worker( a, b ).run();
}

This approach has some benefits: 这种方法有一些好处:

  1. You can test those workers in isolation. 您可以孤立地测试那些工人。
  2. It keeps code together that belongs together 它使属于一起的代码保持在一起
  3. It helps to avoid cluttering the namespace of a class. 它有助于避免混乱类的名称空间。 It does pollute the global namespace somewhat, though. 但是,它确实会污染全局名称空间。
  4. It allows you to reuse workers in different classes. 它允许您重用不同类中的工作器。
  5. I can lessen the restrictions on fields for workers. 我可以减少对工人领域的限制。 For main classes, I prefer fields that don't change. 对于主类,我希望不要更改字段。 In workers, fields are often more like local variables. 在工作者中,字段通常更像是局部变量。 That way, I can reduce the number of method parameters but I need to write more unit tests. 这样,我可以减少方法参数的数量,但是我需要编写更多的单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM