Sunday, June 14, 2009

Guice, Read only POJO, and Builders

I often design java interfaces with methods that return POJOs with read-only properties like the following.


public interface Manager {
      public Info lookupInfo( String sKey );
}

public interface Info {
    public String getA();
    public int    getB();
}

I prefer to decouple Manager's implementation from Info's implementation via Guice injection like this:

public class SimpleManager implements Manager {
    ...
    @Inject
    public SimpleManager( Provider<Info> provideInfo ) { ...
}

The problem with injecting the Info provider is that provideInfo supplies read-only objects which SimpleManager cannot assign property values to. One fix to this problem is to introduce an Info builder interface, and inject that, and a nice way to implement the Builder is to nest the Builder interface within the Info interface, then nest the Info implementation within the Builder implementation. We wind up with something like this:

public interface Info {
    ...
    @ImplementedBy( SimpleInfoBuilder.class )
    public Interface Builder {
        public Builder putA( String val );
        public Builder putB( int val );
        public Info build();
    }
}

public class SimpleInfoBuilder implements Info.Builder {
    private static class SimpleInfo implements Info {
           ...
    }
    ...

    public Info build() { return new SimpleInfo( ... ); }
}


public class SimpleManager implements Manager {
    ...
    @Inject
    public SimpleManager( Provider<Info.Builder> provideInfo ) { ...
}


No comments: