简体   繁体   English

Setters vs Java中的重载构造函数

[英]Setters vs Overloaded constructors in Java

I am not sure if a similar question has been asked before, searched for it, but did not get any helpful answers. 我不确定之前是否曾询问过类似的问题,搜索过它,但没有得到任何有用的答案。

As the question suggests, what is better, having an overloaded constructor or having multiple setter functions? 正如问题所暗示的那样,有什么更好的,有重载的构造函数或具有多个setter函数?

Scenario: 场景:

public class Something {

    private int a;
    private int b; 

    public Something(int a, int b) {
        this.a = a;
        this.b = b;
    }
    ... // Do Something
}

Now, my basic requirement was for to have two parameters only. 现在,我的基本要求是只有两个参数。 Now tomorrow, the requirement is changed and I am asked to add a new parameter, c and then the next day d, and given a statement saying we can have more fields. 现在明天,需求被更改,我被要求添加一个新参数,然后是第二天d,并给出一个声明说我们可以有更多字段。

I already have dependency for this constructor in multiple projects. 我已经在多个项目中对此构造函数具有依赖性。 Now, back to my question 现在,回到我的问题

  • Is it advisable to keep adding the new fields to the already overloaded constructor? 是否可以继续将新字段添加到已经重载的构造函数中?
  • Create a new overloaded constructor every time I need to add a new field so that I don't break dependent code? 每次我需要添加一个新字段时创建一个新的重载构造函数,这样我就不会破坏依赖代码?
  • Or simply use the default empty default constructor and use setters only (messing up my immutability, which is not of high concern) 或者只是使用默认的空默认构造函数并仅使用setter(弄乱我的不变性,这不是高度关注)

What is the advice that you can give me? 你可以给我什么建议?

最愉快的方法是继续将字段添加到构造函数中 - 拥有setter意味着你不能拥有不可变对象,并且不可变对象总是很好 - 但可能会调查构建器模式 ,这可以帮助你限制你自己只是一个被构建器对象调用并“填充”的构造函数。

The good thing about a constructor, as opposed to setters, is that it allows you to enforce the setting of required properties for an instance, rather than having the object be in a bad state until its correct setters are called. 与setter相反,构造函数的优点在于它允许您强制设置实例所需的属性,而不是让对象处于错误状态,直到调用正确的setter为止。 Also, as the other posters mentioned, immutability can be a very good thing, particularly in a multi-threaded context. 此外,正如其他海报所提到的,不变性可能是一件非常好的事情,特别是在多线程环境中。

Nevertheless, your instincts are correct: constructors can grow unwieldy. 然而,你的直觉是正确的:构造者可能变得笨拙。 To second the other posters yet again, the builder pattern can give you the best of both worlds in this situation. 为了再次推出其他海报,在这种情况下, 构建器模式可以为您提供两全其美的效果。 If you don't want the builder to be a nested class of the product, as it is depicted in the Java example in the Wikipedia article, then just put it in the same package as the product, and give the product package-protected setters. 如果您不希望构建器成为产品的嵌套类,如Wikipedia文章中的Java示例所示,那么只需将其放在与产品相同的包中,并为产品包提供保护的setter 。 Also, you can add logic to enforce the setting of mandatory properties when the caller declares building to be complete. 此外,您可以添加逻辑以在调用者声明构建完成时强制执行强制属性的设置。

The objective of having different constructors is to increase the reusability of the class. 拥有不同构造函数的目的是提高类的可重用性。 I think it will be more helpful to have a few different constructors that serve to your needs rather than having a lot of setters. 我认为有一些不同的构造函数可以满足您的需求而不是拥有大量的setter会更有帮助。 Also the constructors are more specific and improve the readability of your class and the api. 此外,构造函数更具体,并提高了类和api的可读性。

Do your other projects that depend on 2-arg constructor benefit in any way from new parameters? 您的其他依赖于2-arg构造函数的项目是否会以任何方式从新参数中受益? Do 2-arg constructors make sense with your new requirements? 2-arg构造函数是否符合您的新要求?

Maybe you need to create another class, eg SomethingEx(Something) which would carry additional fields and have different constructors, but would share useful methods. 也许你需要创建另一个类,例如SomethingEx(Something) ,它将携带额外的字段并具有不同的构造函数,但会共享有用的方法。

If useful methods to share are few and very short, it may be better to create an entirely different class, just to have fewer dependencies. 如果共享的有用方法很少而且很短,那么创建一个完全不同的类可能会更好,只是为了减少依赖。

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

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