oliviercoma / fluent-builders-generator-eclipse-plugin

Automatically exported from code.google.com/p/fluent-builders-generator-eclipse-plugin
0 stars 0 forks source link

Change existing instances with fluent interfaces #10

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello developers,

I'm new with fluent interfaces but want to ask for an enhancement,
if this makes sense.

Assuming we have a bean/pojo class (e.g. Person) and have
generated an instance (conventionell or with the fluent interface 
"PersonBuilder") like this:

Person p = 
PersonBuilder.person().withFirstName("Ula").withLastName("Uvula").withAge(99).wi
thActive(true).build();

If we want to modify the already create instance in a later situation,
it would be nice to use the fluent interface like this:

p.modify().withAge(33).withActive(false);

To enable this, the POJO class must extended by a method

public PersonBuilder modify () {
  return new PersonBuilder(this);
}

and the generated code must get a second constructor.

Does this make sense?

Ula

Original issue reported on code.google.com by ula.uvula@gmx.de on 22 Sep 2010 at 10:42

GoogleCodeExporter commented 8 years ago
Nice idea - we were thinking about something more, less similar: 
PersonBuilder.person(instance).....
, but in my opinion it should return a copy of the existing object (what is 
quite complicated, cause You don't always know how to copy some fields or You 
should omit some). The main idea of our plugin is not to be intrusive on an 
existing code (actually it comes from the fact that most of the time we use it 
for fixturing the POJO's for testing and sometimes code of POJO's are in the 
other artifact...)

This seems like a great idea, but still need some thinking and coding things 
we've already planned :).

Thanks a lot for the idea, hope our plugin is helpful.

(attaching pawelw, he has more thoughts about that AFAIR).

Original comment by kube...@gmail.com on 23 Sep 2010 at 5:56

GoogleCodeExporter commented 8 years ago

Original comment by kube...@gmail.com on 23 Sep 2010 at 1:07

GoogleCodeExporter commented 8 years ago
Hello "kubek2k".

1.)
I like the idea with the 
PersonBuilder.person(instance).with...
So you don't have to put code into the pojo/bean class and don't make any
unnecassary dependencies in "Person".

2.) Copy or not:
Maybe also a good idea? Offering 3 fluent if constructors like this:
PersonBuilder.create().with...           (create a new insance)
PersonBuilder.modify(instance).with...   (modify the instance)
PersonBuilder.clone(instance).with...    (your idea, return a copy).

3.) 
Also a nice idea, but lot of work:
the plugin can give you warnings, when a pojo was modified (new setters added) 
and
the generated fluent interface is not acutal (out-of-sync), and the user can 
recreate
the fluent interface.

The easiest solution is:
having a naming convention: Person -> PersonBuilder.
If Person is newer then PersonBuilder must be recreated.

4.)
The performance is realy good, because the plugin is generating code.
Only ~ 40-50% slower then to use the bean methods.

If you look to ideas like
  http://codemonkeyism.com/fluent-interface-and-reflection-for-object-building-in-java/
This solutions are 100x (times) slower the your plugin.

Thanks for your feedback

Ula

Original comment by ula.uvula@gmx.de on 23 Sep 2010 at 5:46

GoogleCodeExporter commented 8 years ago
2) nice idea, but it stops being a real builder, but worth thinking about

3) I don't why, but You're not the first person that asks for that :). The 
problem is that it would make the plugin too complicated, intrusive and 
additionally it would hurt IDE performance a lot. My point of view on that is - 
if You add a field to the POJO, and You need to change the builder 
respectively, You just regenerate the builder. I'm not sure whether effort put 
into the more complicated solution would percent.

4) Nice to hear that - funny thing is, that internally builders that You've 
shown are more interesting, but the problem is that it's quite hard to maintain 
it, especially when You want to have'em similar features, as ours.

thats my 2p :)
Kuba 

Original comment by kube...@gmail.com on 24 Sep 2010 at 11:59