jeudi 2 avril 2015

Which is the best way to use Guice 3.0 with MVC/MVP approach on Java Swing?

I am using the Guice 3.0 with Model-View-Presenter (MVP) to produce low coupling architecture (design) in Java Swing (quasi-MVP). I calling of the quasi-MVP because it is first stage to do this architecture (in this case, does not includes a refined view point yet). To do it, I created the packages below with DI – Decency Injection.



  • Presenter – consist a set of class to control access to lower layers such as Services.

  • View – consist in an set of class to represents the view (UI) elements

  • Model – a set of class to represents DTOs - Data Type Objects

  • Service – a model service layer which contains a set of methods to retrieve DTOs

  • Injectors – represents a set of useful stuffs for Guice inject dependency


My intention, it is to use Guice with Swing to have DI where I need. In addition, this architecture has some configurations files such as POMs and JNLPs, not showed. Also, this application is start from Java Webstart Application using JNLP.


So, my question is what is the best way to perform this architecture with Guice for small apps ? In addition, I have some doubt points, in particular if the AppPresenterFacade is a correct pattern and approach to avoid tight coupling as well as the package hierarchy defined. However, I would like to know some tips and suggestions. See the code below:


PS: I checked some questions related, such as How to use Guice in Swing application but I can't find issues involving design and architecture.





public class MainApp {
public static void main(String args[]) {
initPropertiesFromJnlp( args );
showApp();
}
// ... ...
private static void showApp() {
AppFrameView view = AppFrameView.getInstance(AppFrameView.class);
AppPanel panel = AppPanel.getInstance(AppPanel.class);
view
.addComponent( panel )
.showView();

}
}
}





public class AppPresenterImpl implements AppPresenter{


private final AppServices services;


@Inject
public AppPresenterImpl (AppServices services) {
this.services = services;
}

public void handleOkButton(final ActionEvent actionEvent){
new Thread(
new Runnable() {
@Override
public void run() {

// call service from DI
}
}
).start();
}

public void handleCancelButton(final ActionEvent actionEvent) {
System.exit(0);
}

}





public class AppIServicempl implements AppServices {
// some code from service
public void dispatchAppDoSomething() {}
}





public class AppView extends JFrame {
private JLabel lblSomeThing;

@Inject
public AppView(AppPresenter presenter) {
this();
this.presenter = presenter;
}
// more code
}





public class AppPresenterFacade {
private AppPresenterFacade(){}


public static <T> T getInstance(java.lang.Class<T> clazz){
Injector injector = Guice.createInjector( new AppModule() );
T instance = injector.getInstance(clazz);
return instance;
}
}





public class AppModule extends AbstractModule {


@Override
protected void configure() {
bind(AppServices.class).to(AppServicesImpl.class);
bind(AppPresenter.class).to(AppPresenterImpl.class);
}
}

Aucun commentaire:

Enregistrer un commentaire