Re: News for Java?
Arne Vajh??j wrote:
Setting state in constructor is nice.
But usage of factories and DI (without support for
constructor injection) may make it impossible to use.
Factories can set immutable public state for their clients. They control the
constructors, hiding them from the outside, so their use is compatible with
setting state in constructors of the hidden implementation classes.
public interface Fooster
{
public Condition getInitialCondition();
public void foo();
}
public class FoosterFactory
{
public static Fooster produce( Condition condition )
{
return new FoosterImpl( condition );
}
}
public class Client
{
public void doSomething()
{
Condition condition = createInitialCondition();
Fooster fooster = FoosterFactory.produce( condition );
fooster.foo();
}
}
The DI situation is similar to the resource-handler situation you mentioned
elsethread. The immediate client layer needs to trust (but verify) the DI and
protect its own clients from violations of that trust. The injected resource
might have mutable public state, but the direct consumer of that resource
might not.
The underlying principle throughout this subthread is to design public state
very carefully, whether mutable, observable or both.
--
Lew
Ceci n'est pas une pipe.