What is Deferred Binding?

Deferred Binding is the Google Web Toolkit answer to Java reflection.

It's easiest to explain Deferred Binding by starting with a use case.  Every web browser has its own idiosyncrasies—usually lots of them! (The sheer unmanageable number of them is the problem GWT was created to solve in the first place.)  The standard Java way of dealing with idiosyncrasies would be to encapsulate the custom code into subclasses, with one subclass for each supported browser.  At runtime, the application would use reflection and dynamic classloading to select the appropriate subclass for the current environment, load the class, create an instance, and then use that instance as the service provider for the duration of the program.

This is indeed what GWT does.  However, the JavaScript environment in which GWT applications ultimately run simply does not support dynamic classloading (also known as dynamic binding.)  You can certainly include code to support each browser in your generated JavaScript code, but to do so you must include support for all browsers is in the single application file.  Why should an Opera user have to download code specific to Firefox, when there is no chance at all that she will ever need it?

Because dynamic binding is unavailable as a technique to GWT, GWT instead uses deferred binding.  One way to think of this is as "dynamic class-loading that occurs at compile time instead of execution time."  When the GWT Compiler compiles your Java application, it determines all the different "idiosyncrasies" that it must support, and generates a separate, tightly streamlined version of the application for that specific configuration.  For example, it generates a different version of the application file for Firefox than it does for Opera.

There's more to it than just browser detection, though.  Deferred Binding is a fully generic mechanism for handling features that vary according to some context.  Another classic example of Deferred Binding is Internationalization:  the GWT Compiler uses Deferred Binding to generate a completely separate version of the application for each language.  Why should an English speaker have to download the French text of your application?

Browser version and language represent two "axes" of variance for your application.  It is possible to define your own axis, if you need to, and the GWT compiler handles all the gory details of generating all the possible permutations.  For instance, if GWT supports 4 browsers, and you write your application in 3 languages, then GWT will generate a total of 12 different permutations of your application.  During bootstrapping at runtime, GWT picks the appropriate permutation to show the user.

It's probably pretty clear by now that Deferred Binding works a little differently than standard dynamic binding.  Conceptually, though, the two concepts are fairly similar, and in practice all you usually need to do is just substitute a GWT method for a Java Reflection method.  For example, instead of obj.getClass().getName(), you use GWT.getTypeName(obj).  Instead of Class.forName("MyClass") you use GWT.create(MyClass).  GWT handles all the details of managing the permutations.

Deferred Binding is one of the key features that lets GWT produce high-quality, well-optimized JavaScript code.  If you're just using GWT, you usually won't have to get very far into the details.  If you do have questions, though, the Google Web Toolkit Discussion Group is ready to help!
Google apps
Main menu
Search Help Center
true
true
false
false