Are Java Beans as data storage classes bad design?

后端 未结 6 1534
挽巷
挽巷 2021-02-14 11:49

Usually JavaPractices.com is a good site with good idea\'s, but this one troubles me: JavaBeans are bad.

The article cites several reasons, mainly that the term JavaBea

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 12:30

    The main reason for avoiding setters is immutability. Code for immutability up front and you avoid any threading issues around those objects.

    If you end up with a constructor that reads

    new Person("param 1","param 2","param 3","param 4","param 5","param 6","param 7","param 8")

    then your object is too complicated. You will need Parameter objects (See the Martin Fowler Refactoring book).

    Code defensively at the start and the people who come along and maintain your code will either thank you (good) or curse you (because they can't be lazy and just mutate objects).

    When you need to change the object, add a copy constructor (i.e. clone method). Modern JVMs deal with this easily and quickly enough and there is little speed penalty. You also make things easy for Hotspot and the GC.

提交回复
热议问题