I use the Builder pattern for several classes of my project (multiple arguments, some mandatory, some optional, etc.). These classes are immutable (no setters, deep copy for collections getters).
I am now trying to store those objects in a database using a persistence framework that constructs objects with default constructor + setters. It doesn't like my Builders very much!
I don't want to degrade that setup to POJOs and lose the advantages of the current design (flexibility, immutability, construction security).
I would welcome any feedback on workarounds that can be used in this situation (I could wrap each of these classes but that would double the number of classes and I would rather avoid that).
One post actually points that as a specific disadvantage of the Builder pattern.
EDIT
One answer proposes to use private constructor / setters but that only works if the fields of the class are not final, which is not my case.
FINAL EDIT
Thanks to all.
What I think will be my final solution looks like this and works fine (for the record, I'm using MongoDB + Morphia):
class AClass {
private final String aField;
private final AClass() {
aField = "";
}
//Standard builder pattern after that - no setters (private or public)
}
As I said in my comment: you can include a default constructor and all required setters but make them private. This way you maintain the immutability of your Objects but an ORM such as Hibernate will be able to access the methods/constructor when it needs to.
Anybody else would be able to access these methods using reflection too, but then they can access the private member variables using reflection too. So there is no real downside to adding the private methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With