On 02/16/2011 03:19 PM, Kevin Wright wrote:



Turtles all the way down!  'tis a good philosophy :)

Constructor injection is the way to go:

    public class A {
        public A(final B b) { this.b = b }
        public final B b;
        public final Object prop = new Object;
    }


and already, the implementation has cost you far fewer lines, I certainly know which one I'd rather maintain!

The real beauty of having everything immutable here is that you can then start sharing structures:

    public class A {
        public A(B b, object prop) { this.b = b; this.prop = prop; }
        public final B b;
        public final Object prop;
        public withNewProp(Object newprop) {
            return new A(this.b, newprop);
        }
    }

There's no concerns sharing b like this if it's immutable, so you don't have to worry about making a defensive deep copy to avoid any risk that the instance of B might be mutated via the original i instance of A.
I don't see how this solves my example. You're injecting prop in the constructor of A. In my example, prop is computed also by B. This means that the caller of the constructor of A must know how to compute B. It's not what I want to do...

--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
java.net/blog/fabriziogiudici - www.tidalwave.it/people
[email protected]

--
You received this message because you are subscribed to the Google Groups "The Java 
Posse" 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/javaposse?hl=en.

Reply via email to