This commit is contained in:
2016-08-01 23:30:21 -04:00
parent 2b94a9fad4
commit 5706837132
14 changed files with 417 additions and 4 deletions

View File

@@ -0,0 +1,10 @@
package com.acme;
public class GpsSensor {
public GpsSensor() {
}
public void calibrate() {
}
}

View File

@@ -0,0 +1,55 @@
package com.example;
import javax.inject.*;
import dagger.*;
import com.acme.*;
@Module
class YahooWeatherModule {
private final String key;
public YahooWeatherModule(String key) {
this.key = key;
}
@Provides
WeatherService provideWeatherService(WebSocket socket) {
return new YahooWeather(key, socket);
}
}
@Module
class WeatherChannelModule {
@Provides
WeatherService provideWeatherService() {
return new WeatherChannel();
}
}
@Module
class GpsSensorModule {
@Provides
GpsSensor provideGpsSensor() {
GpsSensor gps = new GpsSensor();
gps.calibrate();
return gps;
}
}
@Component(modules = {YahooWeatherModule.class, GpsSensorModule.class})
interface AppComponent {
WeatherReporter getWeatherReporter();
}
public class Application {
public static void main(String args[]) {
String apiKey = args[0];
YahooWeatherModule yahoo = new YahooWeatherModule(apiKey);
AppComponent component = DaggerAppComponent.builder()
.yahooWeatherModule(yahoo)
.build();
WeatherReporter reporter = component.getWeatherReporter();
reporter.report();
}
}

View File

@@ -0,0 +1,14 @@
package com.example;
import javax.inject.*;
import com.acme.*;
public class LocationManager {
private final GpsSensor gps;
@Inject
public LocationManager(GpsSensor gps) {
this.gps = gps;
}
}

View File

@@ -0,0 +1,8 @@
package com.example;
import javax.inject.Inject;
public class WeatherChannel implements WeatherService {
@Inject
public WeatherChannel() {
}
}

View File

@@ -0,0 +1,22 @@
package com.example;
import javax.inject.*;
public class WeatherReporter {
private final WeatherService weatherService;
private final LocationManager locationManager;
@Inject
public WeatherReporter(WeatherService weatherService,
LocationManager locationManager) {
this.locationManager = locationManager;
this.weatherService = weatherService;
}
public void report() {
// locationManager.getCurrentLocation()
// weatherService.getConditionsAt(currentLocation)
System.out.println("Mostly clouded, 26 C\n");
}
}

View File

@@ -0,0 +1,5 @@
package com.example;
import javax.inject.*;
public interface WeatherService {
}

View File

@@ -0,0 +1,8 @@
package com.example;
import javax.inject.Inject;
public class WebSocket {
@Inject
public WebSocket() {
}
}

View File

@@ -0,0 +1,14 @@
package com.example;
import javax.inject.Inject;
public class YahooWeather implements WeatherService {
private final WebSocket socket;
private final String key;
public YahooWeather(String key, WebSocket socket) {
this.key = key;
this.socket = socket;
}
}