This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CleanArchitectureApplication extends Application{ | |
private static BaseComponent sBaseComponent; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
ApplicationComponent applicationComponent = DaggerApplicationComponent | |
.builder() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public BaseComponent build() { | |
if (localModule == null) { | |
this.localModule = new LocalModule(); | |
} | |
if (networkModule == null) { | |
this.networkModule = new NetworkModule(); | |
} | |
if (applicationComponent == null) { | |
throw new IllegalStateException("applicationComponent must be set"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(dependencies = {BaseComponent.class}) | |
public interface ActivityInjectorComponent { | |
void inject(MainActivity activity); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Inject | |
TweeterApi tweeterApi; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dagger 2 implementation of a module | |
@Module | |
public class MyModule { | |
@Provides | |
public MyDependency providesMyDependency(){ | |
return new MyDependency(); | |
} | |
@Provides |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject //Constructor Injection | |
public AnotherClass(MyDependency myDependency){ | |
} | |
@Inject //Field Injection | |
MyDependency myDependency; | |
@Inject //Method Injection | |
public void myMethod(MyDependency myDependency){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AnotherClass { | |
private final MyClass myClass; | |
public AnotherClass(MyClass myClass){ | |
this.myClass = myClass; | |
} | |
} | |
public class Main{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DependencyFactory{ | |
private static MyDependency myDependency; | |
private static MyClass myClass; | |
public static MyDependency providesMyDependency(){ | |
if(myDependency == null){ | |
myDependency = new MyDependency(); | |
} | |
return myDependency; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AnotherClass{ | |
private final MyClass myClass; | |
//Good! | |
public AnotherClass(MyClass myClass){ | |
this.myClass = myClass; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDependency {} | |
public class MyClass { | |
private MyDependency dependency; | |
public MyClass(MyDependency myDependency){ | |
this.dependency = myDependency; | |
} | |
} |