Why shouldn't I use immutable POJOs instead of JavaBeans?

前端 未结 7 1333
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 06:27

I have implemented a few Java applications now, only desktop applications so far. I prefer to use immutable objects for passing the data around in the application instead of

7条回答
  •  庸人自扰
    2020-12-04 06:55

    I don't think immutable objects will get all that popular, to be honest.

    I do see the advantages, but frameworks like Hibernate and Spring are currently very much in vogue (and for a good reason too), and they really work best with beans.

    So I don't think immutability is bad, but it would certainly limit your integration options with current frameworks.

    EDIT The comments prompt me to clarify my answer a bit.

    There most certainly are problem areas where immutability is very useful, and is indeed used. But I think the current default seems to be mutable as that is what is mostly expected, and only immutable if that has a clear advantage.

    And though it is indeed possible to use constructors with arguments in Spring it seems to be intended as a way to use legacy and/or third party code with you beautiful brand-new Spring code. At least that's what I picked up from the documentation.

提交回复
热议问题