Part 2
This commit is contained in:
10
part-2/src/main/java/com/acme/GpsSensor.java
Normal file
10
part-2/src/main/java/com/acme/GpsSensor.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.acme;
|
||||
|
||||
public class GpsSensor {
|
||||
|
||||
public GpsSensor() {
|
||||
}
|
||||
|
||||
public void calibrate() {
|
||||
}
|
||||
}
|
||||
55
part-2/src/main/java/com/example/Application.java
Normal file
55
part-2/src/main/java/com/example/Application.java
Normal 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();
|
||||
}
|
||||
}
|
||||
14
part-2/src/main/java/com/example/LocationManager.java
Normal file
14
part-2/src/main/java/com/example/LocationManager.java
Normal 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;
|
||||
}
|
||||
}
|
||||
8
part-2/src/main/java/com/example/WeatherChannel.java
Normal file
8
part-2/src/main/java/com/example/WeatherChannel.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.example;
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class WeatherChannel implements WeatherService {
|
||||
@Inject
|
||||
public WeatherChannel() {
|
||||
}
|
||||
}
|
||||
22
part-2/src/main/java/com/example/WeatherReporter.java
Normal file
22
part-2/src/main/java/com/example/WeatherReporter.java
Normal 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");
|
||||
}
|
||||
}
|
||||
5
part-2/src/main/java/com/example/WeatherService.java
Normal file
5
part-2/src/main/java/com/example/WeatherService.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.example;
|
||||
import javax.inject.*;
|
||||
|
||||
public interface WeatherService {
|
||||
}
|
||||
8
part-2/src/main/java/com/example/WebSocket.java
Normal file
8
part-2/src/main/java/com/example/WebSocket.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.example;
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class WebSocket {
|
||||
@Inject
|
||||
public WebSocket() {
|
||||
}
|
||||
}
|
||||
14
part-2/src/main/java/com/example/YahooWeather.java
Normal file
14
part-2/src/main/java/com/example/YahooWeather.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user