Hello World example - suggestions?

376 views
Skip to first unread message

Thorbjørn Ravn Andersen

unread,
Aug 3, 2015, 10:47:26 AM8/3/15
to Dagger Discuss
I am getting acquainted with Dagger 2, and could not find a "Hello World" example, so I've written one (which might be adapted and added to the official documentation)

---
package demo;

import dagger.Component;
import dagger.Provides;
import dagger.Module;

import java.util.concurrent.Callable;

public class HelloWorld {
public static void main(String[] args) throws Exception {
System.out.println(DaggerHelloWorld_OurComponent.builder().build().getCallable().call());
}

@Component(modules = OurModule.class)
interface OurComponent {
Callable getCallable();
}
}

@Module
class OurModule {
@Provides
Callable getHelloWorld() {
return () -> "Hello World";
}
}

---

I've used Callable to keep the class count down.  OurModule and OurComponent is to allow
the import statements.

Have I missed anything, or is this as tight as it can get?  

/Thorbjørn

Jens

unread,
Aug 3, 2015, 12:01:56 PM8/3/15
to Dagger Discuss
While a hello world example should be short it should also be very concrete and understandable. IMHO your naming is too generic and binding Callable is kind of unrealistic so overall I would not like that as a hello world example.

You better create an interface called MessagePrinter and a class ConsoleMessagePrinterImpl that you can bind with dagger. In your main() method you can then do component.printer().print("Hello world"). This also allows you to choose concrete names for your component and module like HelloWorldComponent and ConsolePrinterModule.

-- J.

Thorbjørn Ravn Andersen

unread,
Aug 4, 2015, 6:38:35 AM8/4/15
to Dagger Discuss
Duly noted.  Perhaps you would like to adapt it so we have a complete example as you would like it to be?

Jens

unread,
Aug 4, 2015, 2:20:45 PM8/4/15
to Dagger Discuss

Duly noted.  Perhaps you would like to adapt it so we have a complete example as you would like it to be?

I guess the following would be my Hello World!

package com.example.dagger;

import dagger.Component;
import dagger.Module;
import dagger.Provides;

public class Example {

  interface Printer {
    void printMsg(String msg);
  }

  static class ConsolePrinter implements Printer {
    @Override
    public void printMsg(String msg) {
      System.out.println(msg);
    }
  }

  @Component(modules = ConsoleModule.class)
  interface HelloWorldApp {
    Printer getPrinter();
  }

  @Module
  static class ConsoleModule {
    @Provides
    Printer providePrinter() {
      return new ConsolePrinter();
    }
  }

  public static void main(String[] args) {
    HelloWorldApp app = DaggerExample_HelloWorldApp.create();
    app.getPrinter().printMsg("Hello World!");
  }
}


-- J. 

Thorbjørn Ravn Andersen

unread,
Nov 9, 2017, 9:14:40 AM11/9/17
to Dagger Discuss
Thanks for the response.   Several revisions later the result is on https://github.com/ravn/dagger2-hello-world 
Reply all
Reply to author
Forward
0 new messages