Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable objects and Spring/Spring MVC: the right choice?

I usually try to design my classes as immutable classes, so I have a lot of advantages in terms of coding stress.

But working with Spring, I sometimes notice that the framework "discourages" this kind of design in most cases, in favour of classic JavaBeans design: default constructor + getters/setters.

I really don't like JavaBean designed objects because of their insane mutability. So I'm wondering if I'm missing something...

I try to keep my classes design as elegant and reusable as possible, but a framework requires to change this design or allow it in a difficult way...

What's wrong with this?

like image 684
davioooh Avatar asked Sep 08 '14 08:09

davioooh


People also ask

In which situations is it better to use an immutable object?

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered more thread-safe than mutable objects.

Should you use immutable objects?

Immutable objects offer a number of advantages for building reliable applications. As we don't need to write defensive or protective code to keep application state consistent, our code can be simpler, more concise, and less error-prone than when we define mutable objects.

Why would you want an immutable object?

Immutable objects are thread-safe so you will not have any synchronization issues. Immutable objects are good Map keys and Set elements, since these typically do not change once created. Immutability makes it easier to parallelize your program as there are no conflicts among objects.

Are immutable objects memory efficient?

If we used an immutable type instead, then different parts of the program could safely share the same values in memory, so less copying and less memory space is required. Immutability can be more efficient than mutability, because immutable types never need to be defensively copied.


1 Answers

Well for web form databinding (ie form POST) the issue is that Java reflection is weak on constructors thus it is difficult to do databinding without annotations. A long time ago I contemplated filing a bug that Springs data binding should leverage the oft forgotten @ConstructorProperties (iirc I looked into doing the patch myself but it was rather complicated and would break many things). Someone should probably file a feature request.

BTW I'm talking about web databinding (and not dependency injection) because Spring has for a long time had great support for constructor based DI (immutable objects need constructor based injection). In fact I would say constructor based injection or (static method factories) are becoming the preferred practice over traditional getter/setter components (you can see this in many Spring classes changes over the years moving toward final and constructors).

Anyway I was able to do web data binding with immutable objects using Jackson https://gist.github.com/agentgt/4458079

(Although its using Jackson for databinding the request does not have to be in JSON)

You might also want to see Spring Webflow DataBinding to immutable objects via a constructor? where I original came up with the gist and has more info.

like image 145
Adam Gent Avatar answered Nov 11 '22 17:11

Adam Gent