I have a factory which creates actions based on parameters. They implement my Action
interface.
interface Action {
ActionResult execute() throws Exception;
}
class CopyAction implements Action {
...
}
class MoveAction implements Action {
...
}
I have around 30 different actions. My factory looks like this:
public class ActionFactory{
private ClassA dependency1;
private ClassB dependency2;
private ClassC dependency3;
private UtilClass1 utilDependency1;
private UtilClass2 utilDependency2;
@Inject
public ActionFactory(ClassA dependency1, ClassB dependency2, ClassC dependency3, UtilClass1 utilDependency1, UtilClass2 utilDependency2) {
this.dependency1 = dependency1;
this.dependency2 = dependency2;
this.dependency3 = dependency3;
this.utilDependency1 = utilDependency1;
this.utilDependency2 = utilDependency2;
}
Action createAction(ActionParameters parameters, ...) {
switch (parameters.getActionType()) {
case COPY_ACTION:
return new CopyAction(parameters, dependenc1, dependency2, utilDependency1);
case MOVE_ACTION:
return new MoveAction(parameters, dependency3, utilDependency1, utilDependency2);
// many more cases for all the other actions
}
}
}
I use Dependency Injection (weld) in my project and the factory is injected in the classes which need to create actions.
So now I am mixing dependency injection and new
. Furthermore my factory depends on so many different things that are only used in a couple of actions creation processes. Not all dependencies are necessary for all the actions.
I don't know if this is a good approach but some how it does not feel right. Mixing di and new
seems wrong. But I don't know how to do it better. I don't even know if there is a better way of doing this or is my feeling just wrong?