Does dependency injection mean that you don’t ever need the ‘new’ keyword? Or is it reasonable to directly create simple leaf classes such as collections?
In the example below I inject the comparator, query and dao, but the SortedSet is directly instantiated:
public Iterable<Employee> getRecentHires() { SortedSet<Employee> entries = new TreeSet<Employee>(comparator); entries.addAll(employeeDao.findAll(query)); return entries; }
Just because Dependency Injection is a useful pattern doesn’t mean that we use it for everything. Even when using DI, there will often be a need for new. Don’t delete new just yet.