[REVIEW] Make controller instantiation customizable

Greg Brown greg.x.brown at oracle.com
Fri Dec 16 06:11:43 PST 2011


> The problem in DI is that for a given type (MyType) you can have
> multiple registrations.

I see. So a concrete example might be a mock controller vs. an actual controller, or perhaps a controller that uses local storage when not connected to a network vs. one that uses remote storage when connected.

But wouldn't you want to select the appropriate controller in your controller factory? Hard-coding the parameters of the specific controller you want in your markup seems to go against the IOC principle. I think you'd instead want to parameterize your factory so that it injects the appropriate controller, such that the view is never aware of which actual controller is being used. For example:

public interface MyController { … }

public class MockController implements MyController { … }

public class ActualController implements MyController { … }

public class MyControllerFactory {
   private boolean mock;

   public MyControllerFactory(boolean mock) {
       this.mock = mock;
   }
	
   @Override
   public Object getController(Class<?> type) {
       return mock ? new MockController() : new ActualController();
   } 
}

That seems more inline with the spirt of DI than something like this:

<HBox fx:controller="com.foo.MyController; mock=true">
   …
</HBox>

G



More information about the openjfx-dev mailing list