Presentation on theme: "Coding Basics - Deferred Binding. Deferred Binding is a feature of the GWT compiler works by generating many versions of code at compile time, only one."— Presentation transcript:
Coding Basics - Deferred Binding
Defining Deferred Binding Rules There are two ways in which types can be replaced via deferred binding: – Replacement: A type is replaced with another depending on a set of configurable rules. We only cover this – Code generation: A type is substituted by the result of invoking a code generator at compile time.
Deferred Binding Using Replacement Replacement means overriding the implementation of one java class with another that is determined at compile time. For example, this technique is used to conditionalize the implementation of some widgets, such as the PopupPanel.PopupPanel The use of for the PopupPanel class is shown in the previous section describing the deferred binding rules.
Popup.gwt.xml The actual replacement rules are specified in Popup.gwt.xml, as shown below:
Popup.gwt.xml These directives tell the GWT compiler to swap out the PoupImpl class code with different class implementations according to the the user.agent property. ThePopup.gwt.xml file specifies a default implementation for the PopupImpl class, an overide for the Mozilla browser (PopupImplMozilla is substituted for PopupImpl), and an override for Internet Explorer version 6 (PopupImplIE6 is substituted for PopupImpl). Note that PopupImpl class or its derived classes cannot be instantiated directly. Instead, the PopupPanel class is used and the GWT.create(Class) technique is used under the hood to instruct the compiler to use deferred binding.GWT.create(Class)
Example Class Hierarchy using Replacement To see how this is used when designing a widget, we will examine the case of the PopupPanel widget further. The PopupPanel class implements the user visible API and contains logic that is common to all browsers. It also instantiates the proper implementation specific logic using the GWT.create(Class) as follows:GWT.create(Class)