5

I am currently using java beans model for constructing a POJO with many parameters

example:

   Class Person {
   // various fields
   // the getters and setters for the fields
   }

So for constructing Person, I do

Person a = new Person();
// then setting all the fields(around 20) using the setters.

Now i am thinking about switching to java builder pattern for constructing these kinds of data holder objects. I guess builder pattern is better when it comes to readability. So which is better in terms of efficiency and performance?

Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
  • The main advantage of the builder pattern is that it allows to create immutable instances; and as they are immutable, they are automatically threadsafe as well. – fge Feb 28 '14 at 06:29
  • @fge: fine in that way. But what about performance. Does using builder pattern over java beans in the sample case i ve given complicates things? – Aarish Ramesh Feb 28 '14 at 06:34
  • depends on the builder implementation I would say –  Feb 28 '14 at 06:36
  • Complicates in what sense? I don't see that it complicates anything, it is just a different style; as to performance, it should be slightly better, but I don't believe you'll see a noticeable difference – fge Feb 28 '14 at 06:37
  • The main advantage of using a `builder pattern` is that it returns an `immutable instance` of the class and makes the code more readable and easy to understand. it allows the object chaining. read more at this [thread](http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern) – Ankit Kumar Feb 28 '14 at 06:45
  • @fge: I meant complications in a way that separate inner builder class has to be written. Isn't slightly heavy compared to the straight forward way of constructing using java beans?? – Aarish Ramesh Feb 28 '14 at 06:55
  • @fge: Does builder pattern offer better performance compared to java beans? If yes, in what way? – Aarish Ramesh Feb 28 '14 at 06:56
  • 1
    It is just a habit to take; personally I don't use beans, I only use builders (well, even more than that). As to performance, immutable instances are better but as I also said, you'd be hardly pressed to spot it, really – fge Feb 28 '14 at 06:58
  • @fge: okay thanks. Now i have a better idea about using builders! – Aarish Ramesh Feb 28 '14 at 07:04
  • 3
    Performance is irrelevant, really. You'd probably have to create millions of beans to notice a difference when measuring. Use what you find the most readable and maintainable, and care about performance when you really have a performance problems. The problem will probably be caused by excessive IO, bad SQL requests, or O(N^3) algorithms. Not by the way you're building objects. – JB Nizet Feb 28 '14 at 07:26
  • alright.. @JBNizet & fge: I have a scenario wherein i do need them to be mutable. For instance, i get the object details from database, construct the data holder , update it , then again store it in db. In this case, I want the POJO to be mutable. So is there a mutable builder pattern to satisfy the above use case or builder pattern are to be only used for cases where immutability is needed? – Aarish Ramesh Feb 28 '14 at 07:38
  • 1
    No, you can construct any kind of object, mutable or not, with the builder pattern. – JB Nizet Feb 28 '14 at 07:39
  • @JBNizet: I was trying to design a mutable builder pattern but i couldn't come with anything other than the way of providing explicit setters for setting the object parameters. Is there any appropriate design for a mutable builder pattern? – Aarish Ramesh Feb 28 '14 at 09:11
  • If you know how to build an immutable object, building a mutable one can be done exactly the same way. The only difference is that the class provides, or not, methods allowing to change its state after it has been constructed by the builder. – JB Nizet Feb 28 '14 at 09:16

0 Answers0