我应该将函数放在最高类还是最低类中

huangapple go评论131阅读模式
英文:

Should I put functions in highest class or lowest class

问题

以下是您要翻译的内容:

假设我有一个名为 Document 的类。
在 Document 类中,有一个名为 Text 的属性。

Document.java

public class Document {
     private Text text = new Text();
}

假设 Text 类看起来像这样:

Text.java

public class Text {
     // 不管这个方法做什么。
     public void refresh() {...}
}

如果我想要从另一个类中刷新文档中的文本,应该在 Document 类中添加一个 getter 方法,像这样:

Document.java

public class Document {
     private Text text = new Text();
     
     public Text getText() {
          return text;
     }
}

然后通过以下方式刷新文本:

new Document().getText().refresh();

还是采取这种方式更好:

public class Document {
     private Text text = new Text();
     
     public void refreshText() {
          text.refresh();
     }
}

然后通过以下方式刷新文本:

new Document().refreshText();

哪种方式更好呢?

我想这取决于具体情况,但在更复杂的例子中,如果我选择第一种方法,我的代码将变成:

new Document().getFrame().getPanel().getTextBox().getText().refresh();

而使用第二种方法,我的代码将是:

new Document().refreshText();

我不确定哪种更“正确”,提前谢谢!

英文:

Say I have a class called Document.
In the Document class, there is a Text attribute.

Document.java

public class Document {
     private Text text = new Text();
}

Lets say the Text class looks something like this:

Text.java

public class Text {
     // it doesn't matter what this method does.
     public void refresh() {...}
}

If I wanted to refresh the text in the document from another class, should add a getter to the Document class, like this:

Document.java

public class Document {
     private Text text = new Text();
     
     public Text getText() {
          return text;
     }
}

And refresh the text by doing:

new Document().getText().refresh();

Or is it better to do this:

public class Document {
     private Text text = new Text();
     
     public void refreshText() {
          text.refresh();
     }
}

And refresh the text by doing:

new Document().refreshText();

Which one is better to do?

I assume it depends on the situation, but in more complex examples, if I were to do the first method, my code would end up looking like:

new Document().getFrame().getPanel().getTextBox().getText().refresh();

compared to the second method in which my code would look like:

new Document().refreshText();

I'm just not sure which one is more "right", thanks in advance!

答案1

得分: 0

这在很大程度上是品味或个人意见的问题。然而,第一种选项(部分地)暴露了 Document 的内部实现(通过告诉我们它包含一个 Text),以及 Text 的内部实现(通过告诉我们它包含一个 refresh() 方法),并且违反了迪米特法则

如果您不关心暴露内部实现细节,并可能在代码中突破层次和关注点的分离,您可以像在第一个示例中那样链接方法调用。但是,如果您关心的话,您应该选择第二种方法。不过要注意,采用这种方法,您可能会在类中添加一些包装方法,用来调用其他类的方法。无论哪种方式,您都应该仔细设计您的类,并根据需要进行重构。

英文:

It's largely a matter of taste/personal opinion. However, the first option (partially) exposes the internal implementation of Document (by telling us that it contains a Text) and of Text (by telling us that it contains a refresh() method), and violates the Law of Demeter.

If you don't care about exposing internal implementation details and possibly breaking through the separation of layers and concerns in your code, you can just chain method calls like in your first example. But if you care, you should opt for the second approach. Beware, though, that with that approach, you risk bloating your classes with wrapper methods that call the methods of other classes. Either way you should design your classes carefully, and refactor if/as needed.

huangapple
  • 本文由 发表于 2020年10月15日 04:45:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64361255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定