mardi 3 mars 2015

should we avoid to use spring managed bean when it is unnecessary?

Say I have a relative complex class which needs to be simplified by breaking into few smaller helper classes. One suggested refactor solution is :



public class RefactoredComplexClass {
private final Helper1 h1;
private final Helper2 h2;
// Helper1 and Helper2 will be injected by spring IoC
public RefactoredComplexClass(Helper1 h1, Helper2 h2) {
this.h1 = h1; this.h2 = h2;
}
}
public class Helper1 {// no state class
public int add(int x, int y) { return x + y ; }
}


The reason behind above suggestion is mainly for making mockito based test easy.


My question is : 1. Should static method rather than instance method be used for those no state helper class? 2. Is it a good idea to inject those Helper object?


My own thought on this is static method should be used since there is no state to maintain for those helper class/method. And it is unneccessary to inject those helper classes' instances. But I do agree to extent that above solution making mockito based testing easier given the fact that static method is hard to mock.


Any suggestion?


Aucun commentaire:

Enregistrer un commentaire