Java Reference
In-Depth Information
In Listing 5‐6, the UserService constructor asks for an instance of the UserDataRepository
to be “injected” into the constructor. The UserService class is decoupled from the
UserDataReposityImpl class. The factory is now responsible for the creation of the object and
“injects” the implementation into the constructor of the UserService . You have successfully
separated the business logic from object creation.
LISTING 5‐6: The refactored UserService class
package com.devchronicale.di;
class UserService {
private UserDataRepository udr;
UserService(UserDataRepository udr) {
this.udr = udr;
}
public void persistUser(User user) {
udr.save(user);
}
}
WAR STORY
When I was given the task of writing an Android application, I decided to research
Dependency Injection Frameworks for mobiles. As a software developer with
Enterprise Development experience, this seemed like the right route to take. The
Android user interface (UI) system was already reliant on a DI‐like structure that
bound XML‐based UI components to Java code, so it seemed wise to implement a
fully functioning DI framework to achieve blown functionality.
I worked on a beautiful architecture in which all objects and resources were
wired together. The injection worked like a charm; however, the application did
not. The app start‐up took much longer than similar apps, and the navigation
was not so smooth either. We all believed DI was a must‐have to achieve loose
coupling and well‐organized code, so we looked for problems in other areas.
We mastered sleek, lightweight UI and asynchronous background tasks so we
wouldn't lock the app and to minimize the work done on start‐up, but nothing
really worked.
It soon dawned on us that the root of the problem was the DI framework. It was
searching for all injection resources and references, while the app was starting and
trying to perform all the wiring at the beginning of the app's life cycle. This might
be a good idea on a server start‐up, which has many users, few restarts, and huge
amounts of memory. But it wasn't a good idea on a mobile device, where we had a
single user, many restarts, and limited memory.
Search WWH ::




Custom Search