[REVIEW] Make controller instantiation customizable

Roman Kennke roman at kennke.org
Tue Dec 13 23:30:59 PST 2011


Hi,

> >
> > But I am a little confused. In another email you mentioned the desire to
> > not have this association between the FXML file and controller at all (ie:
> > the controller loads the right view, not the other way around). But here
> > you are advocating for the FXML file to load the correct Controller but use
> > a name-based key instead of specifying a class or interface. I guess it is
> > fair to say you want to be able to support both?
> >
> 
> I'm hedging my bets :)
> 
> *Here's the crux:*
> 
> Currently the fx:controller in the FXML is used for two things:
> 
>    1. defining the *API/contract* between the Controller and the FXML and
>    2. providing the *lookup/instantiation* details of the controller
>    implementation

I think in a reasonably clean design you wouldn't want a reference to
the controller or even instantiating the controller in the view. IMO the
view should have no knowledge at all about the controller. My ideal
world would be to have a view that is entirely described by the FXML and
loaded by the controller via FXMLLoader. The view is described as an
interface and the FXMLLoader would use dynamic proxy to implement that
interface. Then the controller would register itself as a listener to
the view and control the view by calling getters and setters. The
advantage is obviuosly:
- Clean separation between controller and view
- Good tooling support (the whole view implementation can be generated
and dynamically loaded at runtime).
- Great testability: you can mock up the view in unit tests for the
controller and you can write nice unit tests for the view component as
well (which would be fairly simple: when that setter is called, control
X changes this property, or such) -- I am obviously envisaging a testing
framework like FEST for Swing, which doesn't exist yet for JavaFX (I am
thinking about starting something like this in ThingsFX).

Now the question is how to achieve this (some of it has been discussed
elsewhere in this thread):
- How to define how control properties / actions map to view
properties/actions for 'outgoing' events to the controller.
- How to define how view setters/getters/properties map to the control
properties.

Regards, Roman




More information about the openjfx-dev mailing list