public class Tweeter { public void tweet(String tweet) { TwitterApi api = new TwitterApi(); api.postTweet("Test User", tweet); } } public class TwitterApi { public void postTweet(String user, String tweet) { HttpClient client = new OkHttpClient(); HttpUrlConnection connection = client.open("...."); /* post tweet */ } }
Tweeter tweeter = new Tweeter(); tweeter.tweet("Hello world!");
public class Tweeter { private TwitterApi api; public Tweeter(HttpClient httpClient) { this.api = new TwitterApi(httpClient); } public void tweet(String tweet) { api.postTweet("Test User", tweet); } } public class TwitterApi { private HttpClient client; public TwitterApi(HttpClient client) { this.client = client; } public void postTweet(String user, String tweet) { HttpUrlConnection connection = client.open("...."); /* post tweet */ } }
Tweeter tweeter = new Tweeter(new MockHttpClient); tweeter.tweet("Hello world!");
Timeline timeline = new Timeline(new OkHttpClient(), "Test User"); timeline.loadMore(20); for(Tweet tweet: timeline.get()) { System.out.println(tweet); }
public class Timeline { String user; TweeterApi api; public Timeline(HttpClient httpClient, String user) { this.user = user; this.api = new TweeterApi(httpClient); } public void loadMore(int n){/*.....*/} public List<Tweet> get(){/*.......*/} }
@Inject
)@Module
/ @Provides
)@Inject
annotation: @Inject private HttpClient client;
@Module
: @Module public class NetworkModule{...}
@Provide
): @Module(injects=TwitterApi.class) public class NetworkModule { @Provides @Singleton HttpClient provideHttpClient() { return new OkHttpClient(); } }
@Inject
annotation@Singleton
tells Dagger to create only 1 client instance and cache it.Injector
, which initializes the graph with one (or more) module. In the context of Android applications, it is most convenient to do this when creating an application (inherit from Application and overload onCreate()
). In this example, I created a TweeterApp class that contains the other components (Tweeter and Timeline) public class Injector { public static ObjectGraph graph; public static void init(Object... modules) { graph = ObjectGraph.create(modules); } public static void inject(Object target) { graph.inject(target); } } public class TweeterApp { public static void main(String... args) { Injector.init(new NetworkModule()); Tweeter tweeter = new Tweeter(); tweeter.tweet("Hello world"); Timeline timeline = new Timeline("Test User"); timeline.loadMore(20); for(Tweet tweet: timeline.get()) { System.out.println(tweet); } } }
public class TwitterApi { @Inject private HttpClient client; public TwitterApi() { // Injector.inject(this); // "" client Dagger' } public void postTweet(String user, String tweet) { HttpUrlConnection connection = client.open("...."); /* post tweet */ } }
Injector.inject(Object)
. This is necessary in order to add a class to the dependency graph. Those. if we have at least one @Inject
in the class - we need to add this class to the graph. As a result, in our graph there should be all classes that request dependencies (each of these classes should make ObjectGraph.inject()
) + modules that satisfy these dependencies (usually added at the stage of graph initialization). @Provides @Singleton HttpClient provideHttpClient() { return new OkHttpClient(); }
@Provides @Singleton HttpClient provideHttpClient() { if(Config.isDebugMode()) { return new MockHttpClient(); } return new OkHttpClient(); }
overrides=true
to the module: @Module(overrides=true, injects=TwitterApi.class) public class MockNetworkModule { @Provides @Singleton HttpClient provideHttpClient() { return new MockHttpClient(); } }
public class TweeterApp { public static void main(String... args) { Injector.init(new NetworkModule(), new MockNetworkModule()); Tweeter tweeter = new Tweeter(); tweeter.tweet("Hello world"); Timeline timeline = new Timeline("Test User"); timeline.loadMore(20); for(Tweet tweet: timeline.get()) { System.out.println(tweet); } } }
//Entry point public class TweeterApp { public static void main(String... args) { Injector.init(new NetworkModule()); Tweeter tweeter = new Tweeter(); tweeter.tweet("Hello world"); Timeline timeline = new Timeline("Test User"); timeline.loadMore(20); for(Tweet tweet: timeline.get()) { System.out.println(tweet); } } } // public class Injector { public static ObjectGraph graph; public static void init(Object... modules) { graph = ObjectGraph.create(modules); } public static void inject(Object target) { graph.inject(target); } } //, Tweeter ( HttpClient ) public class Tweeter { private TwitterApi api; public Tweeter() { this.api = new TwitterApi(); } public void tweet(String tweet) { api.postTweet("Test User", tweet); } } //TwitterApi, HttpClient Dagger'a public class TwitterApi { @Inject private HttpClient client; public TwitterApi() { // Injector.inject(this); // "" client Dagger' } public void postTweet(String user, String tweet) { HttpUrlConnection connection = client.open("...."); /* post tweet */ } } //, HttpClient , ( "" 'injects' ) @Module(injects=TwitterApi.class) public class NetworkModule { @Provides @Singleton HttpClient provideHttpClient() { return new OkHttpClient(); } }
Source: https://habr.com/ru/post/202866/
All Articles