A new command

Let’s add a new command for logging in to the ATM:

final class LoginCommand extends SingleArgCommand {
  private final Outputter outputter;

  LoginCommand(Outputter outputter) {
    this.outputter = outputter;

  public String key() {
    return "login";

  public Status handleArg(String username) {
    outputter.output(username + " is logged in.");
    return Status.HANDLED;

(The abstract SingleArgCommand we’re using for simplicity is defined here).

We can create a LoginCommandModule like our HelloWorldModule to bind LoginCommand as a Command:

abstract class LoginCommandModule {
  abstract Command loginCommand(LoginCommand command);

To start using the LoginCommand in CommandRouter, we’ll replace HelloWorldModule in the @Component annotation with LoginCommandModule. Run the application and try to log in.

This begins to show some of the benefits of using Dagger. With a one line declarative change, we were able to change what Command was received by CommandRouter. CommandRouter had no changes, it just worked. Using this approach, you can write many different versions of your application and reuse code without massive changes.

Previous · Next