|
|
|
|
|
by hinkley
3500 days ago
|
|
The problem has historically been that most J2EE frameworks overly rely on delegation, because of GoF. I don't like seeing the same 5 stack frames in the same class appear three separate times in a single stack trace. Because what does that stack trace say? It says it took Foo five function calls -on itself- to figure out it's not responsible for an operation. So Foo asks Bar to do it, and after half a dozen more function calls someone asks Foo all over again. Who takes 5 function calls to figure out it's STILL not responsible for this action. Lather, Rinse, Repeat. And how do you set a breakpoint in that call tree? Which time do you want to debug it? How many more times was it called before the exception was thrown? |
|
We call the first framework function, which calls the second function with the default values for the arguments that were optional. The second function looks up the application's method using Reflection. Because Reflection code is verbose and has a lot of tricky cases, we want to share code between all places where we call it, so it delegates calling the API in 4 different methods.
Then come up the ServletFilters in an HTTP application, which is an example of pluggable system. If we declare 10 filters, they all do something like check the authentication, gzip the response, check the XSRF token, transform a WebApplicationException into a 404/500 reaponse. Unfortunately they all appear as "ServletFilters" in the stacktrace, until they delegate to the final HTTPServlet itself. Another example of pluggable system is OSGi: Each method call is wrapped in 5 function calls if the method is in another classloader.
There's only one improvement I'm able to imagine: Java could define a pluggable system at the language level, so we don't have to implement them using function calls. This pluggable system stretches for all delegation needs, from OSGi to ServletFilters to apps where the list of delegates were defined by a REST api, etc. Sounds like a big JSR...