简体   繁体   中英

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?

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.

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)

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. 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. 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. Also the constructors are more specific and improve the readability of your class and the api.

Do your other projects that depend on 2-arg constructor benefit in any way from new parameters? Do 2-arg constructors make sense with your new requirements?

Maybe you need to create another class, eg SomethingEx(Something) which would carry additional fields and have different constructors, but would share useful methods.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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