This is perhaps a fork of this thread insomuch as it was what got me thinking again about best ways of doing these things.
Immutable objects are handy since they help with thread-safety and even just semantically since for many objects, if you modify it, it shouldn't be considered the same object. For objects of a somewhat atomic nature, there's no problem, since they get set up with the constructor through a simple operation, and there isn't really even a good notion of what modifying it would mean.
Then there are more complex objects which may have to be constructed through a series of operations, but you still want the end result to be immutable. I've found myself creating FoobarBuilder classes to support incremental construction of these objects. It all gets somewhat messy, since:
1) The FoobarBuilder objects have to eventually dump their contents through a proper Foobar constructor to get what I want, which isn't ideal performance-wise.
2) I see a lot of code duplication, since Foobar and FoobarBuilder share so much in common.
3) I tend to create FoobarBuilder classes locally, since each bit of code that needs to create a Foobar might be building it in a different way, and because FoobarBuilder isn't a part of my global interface, so I end up with multiple FoobarBuilder classes and even more code duplication.
How should I be doing this? I thought about opening up the basic properties of the object so various pieces of code can directly set things, then set a field indicating the object is fully constructed, but I didn't like that for a few reasons:
1) It violates the system I've been trying to maintain, where, if you have a real [non-null] reference to an object, then that represents an object semantically conforming to certain standards, not just structurally conforming in a static-typed way.
2) In my particular cases, the Foobar would contain fixed arrays of data, whereas the FoobarBuilder used Lists or some other dynamically-sized collection, so they aren't really even compatible.
3) It adds an extra field to my Foobar, increasing its complexity and the memory it takes up.
Best practices? Rules of thumb?
Immutable objects are handy since they help with thread-safety and even just semantically since for many objects, if you modify it, it shouldn't be considered the same object. For objects of a somewhat atomic nature, there's no problem, since they get set up with the constructor through a simple operation, and there isn't really even a good notion of what modifying it would mean.
Then there are more complex objects which may have to be constructed through a series of operations, but you still want the end result to be immutable. I've found myself creating FoobarBuilder classes to support incremental construction of these objects. It all gets somewhat messy, since:
1) The FoobarBuilder objects have to eventually dump their contents through a proper Foobar constructor to get what I want, which isn't ideal performance-wise.
2) I see a lot of code duplication, since Foobar and FoobarBuilder share so much in common.
3) I tend to create FoobarBuilder classes locally, since each bit of code that needs to create a Foobar might be building it in a different way, and because FoobarBuilder isn't a part of my global interface, so I end up with multiple FoobarBuilder classes and even more code duplication.
How should I be doing this? I thought about opening up the basic properties of the object so various pieces of code can directly set things, then set a field indicating the object is fully constructed, but I didn't like that for a few reasons:
1) It violates the system I've been trying to maintain, where, if you have a real [non-null] reference to an object, then that represents an object semantically conforming to certain standards, not just structurally conforming in a static-typed way.
2) In my particular cases, the Foobar would contain fixed arrays of data, whereas the FoobarBuilder used Lists or some other dynamically-sized collection, so they aren't really even compatible.
3) It adds an extra field to my Foobar, increasing its complexity and the memory it takes up.
Best practices? Rules of thumb?