“通过引用”编辑对象的良好做法?

发布于 2024-09-10 03:52:52 字数 514 浏览 12 评论 0原文

假设我有一个名为 Superstar 的类型。现在我想要一个方法来完成一些工作并编辑 Superstar 对象的一些属性。

以下是我如何实现这一点的两种方法。方式 1 如下:

private Superstar editSuperstar(Superstar superstar){
    ....
    superstar.setEdited(true);
    return superstar;
}
...
superstar = editSuperstar(superstar);

方式 2 如下:

private void editSuperstar(Superstar superstar){
    ....
    superstar.setEdited(true);
}
...
editSuperstar(superstar);

这两种可能的方式中哪一种被认为是“最佳实践”?第一个,还是第二个伪“通过引用”?

Let's say I've got a type called Superstar. Now I want to have a method that does some work and edits some properties of a Superstar object.

Here are two ways of how I could implement this. Way 1 would be the following:

private Superstar editSuperstar(Superstar superstar){
    ....
    superstar.setEdited(true);
    return superstar;
}
...
superstar = editSuperstar(superstar);

And way 2 would be this:

private void editSuperstar(Superstar superstar){
    ....
    superstar.setEdited(true);
}
...
editSuperstar(superstar);

Which one of these two possible ways is considered "best practice"? The first one, or the second pseudo "by reference" one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无法言说的痛 2024-09-17 03:52:52

就您而言,第二种形式是更可取的,因为您直接更改其中一个超级明星属性(edited)。但是,如果您有一个使用超级明星对象并返回其更新版本(不更改初始版本)的方法,则第一种形式将受到我的青睐。

最后,由于这两个示例都只使用 Superstar 对象,因此它们应该是 Superstar 类的成员方法。

In your case, the second form is preferrable, as you directly change one of you superstar properties (edited). However, if you have a method that use a superstar object and returns an updated version of it (without changing the initial one) the first form will have my favor.

Finally, since both of this examples only use Superstar object, they should be member methods of the Superstar class.

我早已燃尽 2024-09-17 03:52:52

除非您要创建一个要链接调用的“构建器”类,否则请使用方法 2。前任:

MyClass c = (new MyClassBuilder()).setX(blah).setY(blah).build();

Use way 2 unless you are creating a "builder" class where you intend to chain invocations. Ex:

MyClass c = (new MyClassBuilder()).setX(blah).setY(blah).build();
仙气飘飘 2024-09-17 03:52:52

第一种形式是具有欺骗性的。它给人的印象是正在传入一个对象,该对象被复制,然后更改并返回该副本。

“最佳实践”是使用第一种形式,但实际执行隐含的操作(将更改应用于副本,然后返回副本)。不可变对象通常应该优于可变对象,除非它们是大块的东西,复制成本很高,在这种情况下,您应该倾向于第二种形式。

The first form is deceptive. It gives the impression that one object is being passed in, which is copied and the copy then altered and returned.

"Best practice" would be to use the first form, but to actually do what is implied (apply the change to a copy, which is then returned). Immutable objects should generally be preferred over mutable objects unless they are big chunky things that are expensive to copy, in which case, you should then favor the second form.

自在安然 2024-09-17 03:52:52

第一种方法的问题是,如果您像这样使用它:

Superstar edited = editSuperstar(originalSuperstar);

这也会修改originalSuperstar,在我看来,这是违反直觉的...

因此,如果您修改传递的对象,则更喜欢第二个方法,如果您修改传递的对象,则更喜欢第一个方法返回对象的新副本。

对于这个特殊的例子,您可以简单地向 Superstar 类添加一个编辑方法......

The problem you have here with first method is if you use it like that:

Superstar edited = editSuperstar(originalSuperstar);

This will also modify the originalSuperstar which is, in my opinion, counterintuitive...

Thus prefer the second one if you modify the passed object or the first one if you return a new copy of the object.

For this peculiar example you could simply add an edit method to the Superstar class...

世俗缘 2024-09-17 03:52:52

如果您返回仅更改了某些字段的相同实例,第一种形式会让 API 客户端感到惊讶。人们希望在不更改原始实例的情况下获得修改后的副本。

因此,如果您不返回副本,请使用第二种形式;如果您返回副本,请使用第一种形式(并考虑在这种情况下使 Superstar 不可变)。

The first form would be surprising for the API clients if you return the same instance having only changed some fields. One would expect to get a modified copy back without having the original instance changed.

So use the second form if you don't return a copy and use the first if you do (and think about making Superstar immutable in that case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文