I am developing a simulator of a bank software system, where each operation (deposit, withdraw, transfer, ...) is just a string.
I have 5 main classes :
Bank
where bank accounts are stored.
CommandValidator
that takes a command (string) then decides whether it is valid or not (through its validate(string)
method) according to its bank's stored accounts.
public class CommandValidator {
private Bank bank;
public CommandValidator(Bank bank) {
this.bank = bank;
}
public boolean validate(String command) {
//...
}
}
CommandProcessor
that takes a command and executes it (through its process(string)
method), it operates on its bank's stored accounts.
public class CommandProcessor {
private Bank bank;
public CommandProcessor(Bank bank) {
this.bank = bank;
}
public boolean process(String command) {
//...
}
}
CommandStore
that stores the entire system's commands.
public class CommandStore {
private List<String> commands;
public CommandStore() {
commands = new ArrayList<String>();
}
public boolean store(String command) {
commands.add(command);
}
}
In order to conserve the SRP (single responsibility principle), a fifth class named Manager
, must manage the operations between CommandValidator
, CommandProcessor
and CommandStore
, by taking a new command (string), then managing validate, process and store methods' calls in takeCommand(string)
method.
public class Manager {
//...
public Manager(Bank bank) {
//...
}
public void takeCommand(String command) {
//...
}
}
But I want also to conserve the dependency injection, so I can not write the Manager
constructor this way :
public class Manager {
private Bank bank;
private CommandValidator cv;
private CommandProcessor cp;
private CommandStore cs;
public Manager(Bank bank) {
this.bank = bank;
cv = new CommandValidator(bank);
cp = new CommandProcessor(bank);
cs = new CommandStore();
}
}
If I try to inject each object, I will obtain too much parameters in the Manager
constructor :
public class Manager {
private Bank bank;
private CommandValidator cv;
private CommandProcessor cp;
private CommandStore cs;
public Manager(Bank bank, CommandValidator commandValidator, CommandProcessor
commandProcessor, CommandStore commandStore)
this.cv = commandValidator;
this.cp = commandProcessor;
this.cs = commandStore;
}
}
What can I do in order to conserve both SRP principle and DI principle?