Of all the things we do as developers, coming up with a name for an AsyncCallback is probably not the most difficult :)
Overall, I'd say (of course, leaning on the wisdom of others) that there are 2 factors involved with the choice of class location: Visibility Maintainability (i.e. Readability and Reusability) Visibility will generally encourage you to increase coupling (i.e. tend towards anonymous inners), while maintainability would encourage decreased coupling. In reality, you can do it either way by throwing the needed variables into the constructor or whatever. In reality, it boils down to a pure styling thing, since javascript DOES have closures, and I believe that the GWT compiler will make use of them in this instance. I personally default to inner classes, unless I think the Callback could be generic enough to be used by other classes/RPC calls. On Aug 24, 1:40 am, Jan Ehrhardt <[email protected]> wrote: > Once again, the thing you want to do with the callback, is invoking a method > after the request is finished. This can be done very easily with an > annonymous inner class: > public class OuterClass { > > ... > > public void methodForRequest(final int id) { > > service.getSomething(id, new AsyncCallback<Integer>() { > > ... > > onSuccess(Inter result) { > doSomething(id, result); > } > > }); > > } > > private void doSomething(int id, int result) { > ... > } > > } > > This example shows, why annonymous inner classes are great for this use > case. You can tell the 'getSomething' method to invoke the 'doSomething' > method on success. In other languages, you can use closures for such use > cases. The other point is, that the 'id' variable can be passed to the > 'doSomething' method as well. > How would this look, if you're using a non-annonymous inner class or an > outer class? > Furthermore, non-annonymous class require a class name, so you've to think > about a name for it. Do you really want this? > > Regards > Jan Ehrhardt > > On Sun, Aug 23, 2009 at 5:57 PM, Damon Lundin <[email protected]>wrote: > > > > > > > BTW, the terminology you are looking for is that the "inline" inner > > class as you put is called an anonymous inner class while the inner > > class that is defined on its own (not at the point of a method call) > > but still inside the outer class is usually just called an inner class > > but named inner class would also work too. Both of these have been > > available as in Java from the very beginning. > > > As for why, it is just a matter of taste. It's sometimes a bit more > > concise to just define an anonymous inner class inline, particularly > > if the inner class code needs access to variables defined in the > > calling method. With an anonymous inner class you just have to define > > the variables or parameters as final and they can be access in the > > code of the inner class. With a named inner class you have to define > > extra member variable on the inner class to hold those variables and > > probably a constructor that accepts them. > > > If you find that it results in a ton of code inside the inner class > > that all indented, you could just move all that code into a method on > > the outer class. The code in the inner class can access methods and > > methods of the outer class. You can even reference methods and > > methods of the outer class that have the same name as something in the > > inner class by using the syntax: MyOuter.this.outerMethodCall(). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en -~----------~----~----~----~------~----~------~--~---
