不变性和可读性

发布于 2024-09-17 12:41:55 字数 933 浏览 7 评论 0原文

因此,我一直在阅读 Joshua Bloch 的《Effective Java》,并注意到我在工作中实际遇到的两点。

第 1 点:制作 setter 方法以使代码更具可读性。 在他的例子中,我们有一个类,它有一个巨大的构造函数。当人们实例化该类时,很难判断所有参数发生了什么。因此,他建议创建一个简约的构造函数,并为所有其他选项提供 setter 方法,所以不要......

MyClass clazz = new MyClass(a, b, c, d、e、f、g);

你会写....

MyClass clazz = new MyClass(a, b, c);
clazz.setDitto(d);
clazz.setEcho(e);
clazz.setFunzies(f);
clazz.setGumballs(g);

作为可读代码的巨大支持者,我非常喜欢它。

第 2 点:总的来说,他建议使用不可变的类。他深入探讨了为什么拥有不可变的类比拥有可能处于多个不同状态的类要好得多。我可以肯定地说,他把这个想法卖给了我,我迫不及待地想让我从现在开始编写的大多数类都是不可变的,除了……

当你有一个带有巨大构造函数的不可变类时会发生什么?你不能为它创建 setter 方法;这会破坏不变性。我尝试浏览本书的其余部分,但我认为他没有提供解决方案。

一次性使用 setter 方法是有可能的,但事实上,setter 方法可用于被认为是不变性的类,这一事实令人沮丧,即使如果您随后尝试它,它只会抛出异常。

有人对如何处理这个问题有什么好主意吗?我目前在工作中面临这个问题,我有一个带有巨大构造函数的 Immutable 类,我想将其重构为更具可读性而不破坏不变性的东西。

So I've been reading Effective Java by Joshua Bloch and noticed two points which I actually have encountered in my work.

Point 1: Making setter methods to make code more readable.
In his example, we have a class with a ridiculously huge constructor. When people instantiate the class, it's hard to tell what's going on with all the parameters. Thus, he suggested making a minimalistic constructor and have setter methods for all other options, so instead of...

MyClass clazz = new MyClass(a, b, c,
d, e, f, g);

you would write....

MyClass clazz = new MyClass(a, b,
c);
clazz.setDitto(d);
clazz.setEcho(e);
clazz.setFunzies(f);
clazz.setGumballs(g);

Which, as a huge supporter of readable code, I liked a lot.

Point 2: In general, he suggested having immutable classes. He goes into great depth on why having immutable classes is much better than having a class that could be in several different states. I can definitely say that he sold the idea to me, and I can't wait to make most classes I write from now on immutable, except....

What happens when you have an immutable class with a huge constructor? You can't make setter methods for it; that would break immutability. I tried skimming through the rest of the book, but I don't think he covers a solution for this.

There is the possibility of making one-time use setter methods, but just the fact that a setter method is available to a class that is supposedly immutability is disheartening, even if it does just throw an Exception if you try it subsequent times.

Does anyone have any good ideas on how to handle this problem? I'm currently facing this issue at work where I have an Immutable class with a huge constructor which I'd like to refactor to something that's more readable without breaking immutability.

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

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

发布评论

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

评论(4

霊感 2024-09-24 12:41:56

一种选择是提供一个单独的构建器类,该构建器类提供 setter,负责构造实际对象。

在 Bloch 的“Effective Java”第二版中,第 2 项针对不可变类说明了这一点。关键思想是:

  • 构建器为每个选项都有一个可变字段。
  • 构建器将自身作为单个参数传递给不可变类的构造函数。

One option is to provide a separate builder class that provides the setters, which is responsible for constructing the actual object.

In the second edition of Bloch's "Effective Java", item 2 illustrates this for an immutable class. The key ideas are:

  • The builder has a mutable field for each option.
  • The builder passes itself as a single argument to the immutable class's constructor.
找回味觉 2024-09-24 12:41:56

引入参数对象,也许吧?它某种程度上解决了问题,但也许是以有用的方式。您的参数对象不需要任何方法;它只是保存数据,然后你设置它,而不是你真正的类。然后你的真实类通过参数对象在构造函数中初始化自身。

Introduce Parameter Object, maybe? It kind of moves the problem around, but maybe in useful ways. Your parameter object needs no methods; it just holds the data, and you set it up, instead of your real class. Then your real class initializes itself in the constructor via the parameter object.

慢慢从新开始 2024-09-24 12:41:56

拥有一个支持 getter 但不支持该类所有属性的 setter 的抽象基类、一个其构造函数接受基类对象的派生密封“不可变”类以及一个包含所有属性的 setter 的派生可变类怎么样?

How about having an abstract base class which supports getters but no setters for all the attributes of the class, a derived sealed "immutable" class whose constructor accepts a base-class object, and a derived mutable class which includes setters for all the properties?

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