Assuming Guice injects a new MutableObject each time ImmutableObject
is created, then I can see that ImmutableObject would be guaranteed
immutable.
If this was not the case, then I suppose adding a copy() method (or
overriding clone) would be the only way to ensure that it was possible
to get an immutable version of ImmutableObject. I suppose the ideal
solution would be to re-write MutableObject such that it is immutable
(created using a builder). I suppose using Guice I would not even need
the usual build() method:
SomeObject build()
{
return new SomeObject(. , . , .);
}
As you could get Guice to inject the the builder into SomeObject's
constructor directly. Unfortunately, in many situations writing an
immutable version of the class is not practical (java collections for
example). Therefore, in the general case I think your suggestion of
the copy() approach is most appropriate.
Thanks for the reply.
Matt.
On Jan 31, 8:53 pm, Bob Lee <[email protected]> wrote:
> Are you injecting the same MutableObject instance every time? I'd inject a
> new instance every time and forgo the unnecessary extra copy. Or you could
> add a copy() method to ImmutableObject itself (or override clone to make a
> deep copy, return ImmutableObject and not throw an exception).
>
> On Jan 31, 2009 12:27 PM, "Matt G" <[email protected]> wrote:
>
> Hi,
>
> Pre dependency injection, to ensure a class was immutable when it
> takes a mutable object into its constructor, you would create a new
> version of the object before assigning it internally, something
> like...
>
> class ImmutableObject
> {
> private final MutableObject mutObj;
>
> ImmutableObject(MutableObject mutObj)
> {
> this.mutObj = new MutableObj(mutObj);
> }
>
> ...
>
> }
>
> How would this translate to the world of dependency injection? Viewing
> the above class in terms of dependencies; there is clearly a
> dependency on the MutableObject itself, and also a dependency on an
> object that can create a new MutableObject. Assuming we have some sort
> of MutableObjectFactory class, we would write the class as follows:
>
> class ImmutableObject
> {
> private final MutableObject mutObj;
> private final MutableObjectFactory mutObjFac;
>
> @inject
> ImmutableObject(MutableObject mutObj, MutableObjectFactory
> mutObjFac)
> {
> this.mutObj = mutObjFac.create(mutObj);
> }
>
> }
>
> Would this be the recomended way of acheiving Immutablility with
> dependency injection? The problem is particularly apparent when
> injecting collections of objects. As whenever you do this you must
> always copy the contents of the collection to a new collection to
> ensure immutability.
>
> Thanks,
>
> Matt.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---