0

I have a gwt project that acts as a semantic engine for other projects. I recently realized very very little of the code is specific to gwt. Its almost all pretty basic java. In fact, the only things specific to gwt is retrieving files.

So what I would like to do is to separate out the gwt completely so I can use the same basic code for other Java projects - such as Android or Processing apps.

So, "Semantic Core" project could be inherited by GWT,Android and Processing apps and I wont have to maintain separate versions for each.

To do this, however, I need some way for other projects to "give" the Semantic Core project their own file-handleing methods.

My current idea how to do this;

One method I thought how to do this was to have SemanticCore define a Interface for FileManager with a method like;

getFile(String,MyErrorHandler,MySuccessHandler)

And then have the class's for MyErrorHandler and MySuccessHandler defined also in the SemanticCore project, effectively being runnables that take a string as a parameter.

With this Interface defined, other projects (GWT,Android etc) have to define their own class that implements it eg, GWTFileHandler implements FileManager

Then create a object of this class, and pass it to the SemanticCore;

SemanticCore.setFileManager(new GWTFileHandler());

The semantic core can then use it at its leisure to retrieve files in a way suitable for the platform its on.

Question;

  1. Is this a good way to do it? It seems wrong to me I am creating a new object, when I'll only be using static methods from that class.

  2. Alternatives?

I hope my description is clear. As this all has to be GWT compatible in the "SemanticCore" project, any use of reflections is ruled out.

Thanks,

1 Answer 1

0

The recommended approach IMO is to use Deferred Binding to pick the GWT compatible version of your FileHandler or other GWT specific implementations. Extract the common interface for all versions and in your GWT module file you point to correct GWT implementation.

You can then instantiate your specific implemenation using GWT.create :

MyInterface implemenation = GWT.create(MyInterface.class);

more in depth info on the gwtproject site.

Deferred Binding is a technique used by the GWT compiler to create and select a specific implementation of a class based on a set of parameters. In essence, deferred binding is the GWT answer to Java reflection. It allows the GWT developer to produce several variations of their applications custom to each browser environment and have only one of them actually downloaded and executed in the browser.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks thats good to know...allthough that seems a gwt specific solution. I am not sure how, say, an Android/Processing/Other version would pick/supply the file access stuff they need. I put a gwt tag on this as it was the limiting factor, but ideally i am looking for a generic Javamethod to do it.
Interfaces are the solution, and for GWT, this is how you can pick the right implementation. For other platforms you can use dependency injection, have a look at guice or github.com/square/dagger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.