building complex immutable objects

Status
Not open for further replies.

DavlinLotze

Ars Scholae Palatinae
830
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?
 

DavlinLotze

Ars Scholae Palatinae
830
koala":2tl448wn said:
Decompose your object into simpler objects?
By "complex", I'm not meaning all that complicated. The most complicated class I had in mind only has 2 or 3 fields, but one of those fields is an array, and that array is built as a process - for example, by parsing a data file. I don't know from the start all the elements of the array, or even how many there will be, but I have to store what I'm parsing in something in the meantime. Right now, I'm using the *Builder objects I mentioned, but I'm not sure that's the best way to do it.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
What language are you using for this? Since there are very few languages that make "arrays" immutable in and of themselves you are perhaps looking for an impossible ideal.

If your class requires the array to be supplied externally that's fine, expose it as a readonly interface type in your language of choice afterward. If you want to avoid the reference to the array "leaking" to the big wide world, rather than just have the convention that passing the array in to the constructor implies relinquishing all claims to it, then functional techniques can help but not entirely remove the simple fact that you want code external to the class see the array!

If you make the FooBuilder an internal implementation detail to the "real" class and expose the creation of the real class via a static method that defers work to the builder then I see no problem with having the builder. You have closely coupled code, but the construction of it always will be. By doing it this way it is both lexically close (assuming you have the concept of inner classes) and crucially entirely hidden from the outside world so you don't have to worry about anything taking a dependency on it.

If the builder depends on various different possible implementations then the functional technique, commonly refered to in the OO world as Inversion of Control, where you pass in the specific implementation aspect in via a function (or function object if you're in a hamstrung language without higher order functions). Doing it that way keeps the separation of the way you make a Foo nicely immutable with the required invariants from the ways you may supply the data used to make it.
 

DavlinLotze

Ars Scholae Palatinae
830
ShuggyCoUk":2vgtewcg said:
What language are you using for this? Since there are very few languages that make "arrays" immutable in and of themselves you are perhaps looking for an impossible ideal.

It's in C# right now, though that might change down the road. The Foobar contains a collection type I created which only accepts types that either contain no references (e.g. primitives), or types that have "sworn" through one of a couple ways that they themselves are readonly/immutable. Its constructor makes a shallow copy of whatever 1-dimensional collection type is passed in and stores it as a private array, whose members are accessible with an indexer.

Your inversion of control idea is interesting, but I'm thinking it might add unnecessary complication in my case. Foobars can contain Foobars, which means that FoobarBuilders can contain Foobars and FoobarBuilders. I can't enforce strict typing, but the FoobarBuilders are only defined and used locally, so it's not a problem. The FoobarBuilders are actually pretty lightweight right now, though it's a little confusing to have 3 FoobarBuilders defined slightly differently in the same project. The problem I foresee is, as development continues, a FoobarBuilder is likely to need to be addressed, indexed, or manipulated in the advanced ways that are only defined in Foobar.
 
Status
Not open for further replies.