Worst of Singleton and DI

Fri May 15 15:30:00 CDT 2009

I have this on-going rant with Ren. I am fully converted to use of Inversion of Control or Dependency Injection to replace any and all uses of singletons. It makes the classes much easier to test, and there are other benefits described in gory detail elsewhere on the web. I have yet to find a justification for any remaining singletons.

Today, I ran into the following in our code: public class MyLegacyAction extends Action { // a struts Action

private WidgetDao dao;

public MyLegacyAction() { Globals.getInstance().inject(this); }

@Inject public void setMyWidgetDao(WidgetDao dao) { this.dao = dao; } }

This way, we get the worst of the singleton anti-pattern and still...uh..."use" DI.