SlideShare a Scribd company logo
ABUSING
DEPENDENCY INJECTION
WITH DAGGER
FOR DUMMIES
Saúl Díaz (@sefford)
Freakend ‘15
new Flipper(new Gintonic(new Gin(new Alcohol())))
FlipperGintonic GinAlcohol
SHOULD I USE DI WITH MY
PERSONAL/PRO PROJECT?
BASIC DAGGER FOR
DUMMIES
Flipper
Gin
Alcohol
Gintonic
Tonic
Trolling
Code
Google
Engineer
Tears
Android
@Module(
includes = RosieModule.class,
addsTo = {AndroidModule.class,
DrinkModule.class,
CodingModule.class
}
injects = FlipperFragment.class,
complete = true,
)
public class FlipperModule {
@Provides
@Named(“Tuenti”)
public FlipperInterface provideFlipper(Gintonic drink,
Trolling trolling, Android android) {
return new FlipperImpl(drink, trolling,
android);
}
}
public class FlipperFragment extends Fragment {
ObjectGraph graph;
@Inject
FlipperInterface flipper;
@Override
public void onCreate(Bundle bundle) {
graph = ObjectGraph.create(new FlipperModule());
graph.inject(this);
FlipperInterface flipper2 = graph.get(Flipper.class);
}
IDSPISPOPD FOR SINGLETONS
@Singleton
● By definition, Singletons cannot be tested
● Provides same instance of class for Graph
● This instance is not static
ADVANCED DAGGER
FOR DUMMIES
Lazy<Flipper> lazyFlipper;
● Creates the instance on lazyFlipper.get()
● Returns always the same instance (d’oh)
● Always done in UIThread!
LAZY INITIALIZATIONS
PROVIDING INSTANCES
Provider<Flipper> flipperProvider;
● Can help building your own Flipper army
● Returns a different instance each time
@Module(
override = true;
injects = FlipperFragment.class,
complete = true,
)
public class FlipperTestModule {
@Provides
@Singleton
public FlipperInterface provideFlipper() {
return mock(FlipperInterface.class);
}
}
MOCKING INSTANCES
DYNAMIC (SCOPED) INJECTION
public ObjectGraph extend() {
return graph.plus(new ExtensionModule1(),
new ExtensionModule2());
}
STATIC INJECTION
● Reduce boilerplate and mantenibility by
annotating classes and constructions
● Compilation-validated graph
SCOPED INJECTION
● Using a SOLID approach, change
implementations on runtime
● Graph runtime, validated
● Risk of “BOOOOOOM”
ABUSING INJECTION
FOR DUMMIES
CLASSIC EXAMPLE
@Module(injects = LoginActivity.class,
addsTo = AppModule.class)
// @author Antonio Leiva
public class LoginModule {
private LoginView view;
public LoginModule(LoginView view) {
this.view = view;
}
@Provides @Singleton public LoginView provideView() {
return view;
}
@Provides @Singleton
public LoginPresenter providePresenter(LoginView loginView,
LoginInteractor loginInteractor) {
return new LoginPresenterImpl(loginView, loginInteractor);
}
}
WHY STOPPING THERE?public class NoNfcModule() {
@Provides
public NfcInterfaceController provideNfcController() {
return new NullNfcController();
}
}
public class NfcModule() {
@Provides
public NfcInterfaceController provideNfcController(
NfcAdapter adapter) {
return new NfcController(adapter);
}
}
// This goes on Api15Module
@Provides
@Singleton
public StatusBarControllerInterface provideStatusBarControllerInterface() {
return new StatusBarAPI15Controller();
}
// This goes on Api19Module
@Provides
@Singleton
public StatusBarControllerInterface provideStatusBarControllerInterface() {
return new StatusBarAPI19Controller();
}
// This goes on Api21Module
@Provides
@Singleton
public StatusBarControllerInterface provideStatusBarControllerInterface() {
return new StatusBarAPI21Controller();
}
ONE MORE CUP OF ABUSEpublic class ActiveProfileModule() {
@Provides
public ProfilePresenterInterface provideProfilePres(...) {
return new
ActiveProfilePresenter(basePresenter,...);
}
}
public class RegularProfileModule() {
@Provides
public ProfilePresenterInterface provideProfilePres(...) {
return new
RegularProfilePresenter(basePresenter,...);
}
}
QUESTIONS?
THANKS!

More Related Content

What's hot

Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
Pierre-Yves Ricau
 
Dependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutesDependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutes
Antonio Goncalves
 
Vue next
Vue nextVue next
Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02
rhemsolutions
 
CDI: How do I ?
CDI: How do I ?CDI: How do I ?
CDI: How do I ?
Antonio Goncalves
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Ontico
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
Mike Nakhimovich
 
Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2
Kirill Rozov
 
What’s new in Android JetPack
What’s new in Android JetPackWhat’s new in Android JetPack
What’s new in Android JetPack
Hassan Abid
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorialAnh Quân
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized project
Fabio Collini
 
Under the Hood: Using Spring in Grails
Under the Hood: Using Spring in GrailsUnder the Hood: Using Spring in Grails
Under the Hood: Using Spring in Grails
Burt Beckwith
 
継承だろJK
継承だろJK継承だろJK
継承だろJK
makoto tsuyuki
 
Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2
Fabio Collini
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
Visual Engineering
 
Di code steps
Di code stepsDi code steps
Di code steps
Brian Kiptoo
 
MCE^3 - Gregory Kick - Dagger 2
MCE^3 - Gregory Kick - Dagger 2 MCE^3 - Gregory Kick - Dagger 2
MCE^3 - Gregory Kick - Dagger 2
PROIDEA
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
Godfrey Nolan
 
Dependency injection with dagger 2
Dependency injection with dagger 2Dependency injection with dagger 2
Dependency injection with dagger 2
Nischal0101
 

What's hot (20)

Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
 
Dependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutesDependency Injection with CDI in 15 minutes
Dependency Injection with CDI in 15 minutes
 
Vue next
Vue nextVue next
Vue next
 
Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02Architecting your GWT applications with GWT-Platform - Lesson 02
Architecting your GWT applications with GWT-Platform - Lesson 02
 
CDI: How do I ?
CDI: How do I ?CDI: How do I ?
CDI: How do I ?
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
 
Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2
 
What’s new in Android JetPack
What’s new in Android JetPackWhat’s new in Android JetPack
What’s new in Android JetPack
 
Guice tutorial
Guice tutorialGuice tutorial
Guice tutorial
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized project
 
Under the Hood: Using Spring in Grails
Under the Hood: Using Spring in GrailsUnder the Hood: Using Spring in Grails
Under the Hood: Using Spring in Grails
 
Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2Angular Workshop_Sarajevo2
Angular Workshop_Sarajevo2
 
継承だろJK
継承だろJK継承だろJK
継承だろJK
 
Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2Intro to Retrofit 2 and RxJava2
Intro to Retrofit 2 and RxJava2
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Di code steps
Di code stepsDi code steps
Di code steps
 
MCE^3 - Gregory Kick - Dagger 2
MCE^3 - Gregory Kick - Dagger 2 MCE^3 - Gregory Kick - Dagger 2
MCE^3 - Gregory Kick - Dagger 2
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
 
Dependency injection with dagger 2
Dependency injection with dagger 2Dependency injection with dagger 2
Dependency injection with dagger 2
 

Viewers also liked

Danielle\'s Visual Resume
Danielle\'s Visual ResumeDanielle\'s Visual Resume
Danielle\'s Visual Resume
dmhami3
 
Hot skills исследование страховых компаний_2011
Hot skills исследование страховых компаний_2011Hot skills исследование страховых компаний_2011
Hot skills исследование страховых компаний_2011Sergey Onenko
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1cattyloverose
 
สนใจ ไม่ได้แปลว่าได้ใจ
สนใจ ไม่ได้แปลว่าได้ใจสนใจ ไม่ได้แปลว่าได้ใจ
สนใจ ไม่ได้แปลว่าได้ใจ
nudeJEH
 
McCormack -EDIM510- Online Presentation Assignment
McCormack -EDIM510- Online Presentation AssignmentMcCormack -EDIM510- Online Presentation Assignment
McCormack -EDIM510- Online Presentation AssignmentWilkes University
 
Danielle\'s Visual Resume
Danielle\'s Visual ResumeDanielle\'s Visual Resume
Danielle\'s Visual Resume
dmhami3
 
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
itoz itoz
 
Open im vol16_lt_hosi_factory
Open im vol16_lt_hosi_factoryOpen im vol16_lt_hosi_factory
Open im vol16_lt_hosi_factoryNTTDATA INTRAMART
 
Mirsis Corporate Overview
Mirsis Corporate OverviewMirsis Corporate Overview
Mirsis Corporate Overview
Mirsis Bilgi Teknolojileri
 
Presentatie Astorium
Presentatie AstoriumPresentatie Astorium
Presentatie Astorium
Mustafa Cevik
 
Teste
TesteTeste
Agile2015 short paper presentation: Development of Complex Software with Agil...
Agile2015 short paper presentation: Development of Complex Software with Agil...Agile2015 short paper presentation: Development of Complex Software with Agil...
Agile2015 short paper presentation: Development of Complex Software with Agil...
Alan Braz
 
Online Training @ Nitade Chula
Online Training @ Nitade ChulaOnline Training @ Nitade Chula
Online Training @ Nitade Chula
nudeJEH
 
Subsalt Steep Dip Imaging Study with 3D Acoustic Modeling
Subsalt Steep Dip Imaging Study  with 3D Acoustic  ModelingSubsalt Steep Dip Imaging Study  with 3D Acoustic  Modeling
Subsalt Steep Dip Imaging Study with 3D Acoustic Modelingleizhuo
 
2015 Tech Predictions
2015 Tech Predictions2015 Tech Predictions
2015 Tech Predictions
Donnovan Andrews
 
Mirsis Autocontrol İç Kontrol Yönetimi
Mirsis Autocontrol İç Kontrol YönetimiMirsis Autocontrol İç Kontrol Yönetimi
Mirsis Autocontrol İç Kontrol Yönetimi
Mirsis Bilgi Teknolojileri
 

Viewers also liked (20)

Danielle\'s Visual Resume
Danielle\'s Visual ResumeDanielle\'s Visual Resume
Danielle\'s Visual Resume
 
Triconf
TriconfTriconf
Triconf
 
Hot skills исследование страховых компаний_2011
Hot skills исследование страховых компаний_2011Hot skills исследование страховых компаний_2011
Hot skills исследование страховых компаний_2011
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
สนใจ ไม่ได้แปลว่าได้ใจ
สนใจ ไม่ได้แปลว่าได้ใจสนใจ ไม่ได้แปลว่าได้ใจ
สนใจ ไม่ได้แปลว่าได้ใจ
 
McCormack -EDIM510- Online Presentation Assignment
McCormack -EDIM510- Online Presentation AssignmentMcCormack -EDIM510- Online Presentation Assignment
McCormack -EDIM510- Online Presentation Assignment
 
Sampleslideshow
SampleslideshowSampleslideshow
Sampleslideshow
 
Danielle\'s Visual Resume
Danielle\'s Visual ResumeDanielle\'s Visual Resume
Danielle\'s Visual Resume
 
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
2012.7.31 第二回 Stage3D 勉強会「モデリングを始めてみよう」
 
In kor we Trust
In kor we TrustIn kor we Trust
In kor we Trust
 
Open im vol16_lt_hosi_factory
Open im vol16_lt_hosi_factoryOpen im vol16_lt_hosi_factory
Open im vol16_lt_hosi_factory
 
QOL
QOLQOL
QOL
 
Mirsis Corporate Overview
Mirsis Corporate OverviewMirsis Corporate Overview
Mirsis Corporate Overview
 
Presentatie Astorium
Presentatie AstoriumPresentatie Astorium
Presentatie Astorium
 
Teste
TesteTeste
Teste
 
Agile2015 short paper presentation: Development of Complex Software with Agil...
Agile2015 short paper presentation: Development of Complex Software with Agil...Agile2015 short paper presentation: Development of Complex Software with Agil...
Agile2015 short paper presentation: Development of Complex Software with Agil...
 
Online Training @ Nitade Chula
Online Training @ Nitade ChulaOnline Training @ Nitade Chula
Online Training @ Nitade Chula
 
Subsalt Steep Dip Imaging Study with 3D Acoustic Modeling
Subsalt Steep Dip Imaging Study  with 3D Acoustic  ModelingSubsalt Steep Dip Imaging Study  with 3D Acoustic  Modeling
Subsalt Steep Dip Imaging Study with 3D Acoustic Modeling
 
2015 Tech Predictions
2015 Tech Predictions2015 Tech Predictions
2015 Tech Predictions
 
Mirsis Autocontrol İç Kontrol Yönetimi
Mirsis Autocontrol İç Kontrol YönetimiMirsis Autocontrol İç Kontrol Yönetimi
Mirsis Autocontrol İç Kontrol Yönetimi
 

Similar to Dagger for dummies

guice-servlet
guice-servletguice-servlet
guice-servlet
Masaaki Yonebayashi
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
Mathias Seguy
 
Google GIN
Google GINGoogle GIN
Google GIN
Anh Quân
 
Testable Android Apps using data binding and MVVM
Testable Android Apps using data binding and MVVMTestable Android Apps using data binding and MVVM
Testable Android Apps using data binding and MVVM
Fabio Collini
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzg
Kristijan Jurković
 
SOLID
SOLIDSOLID
It's complicated, but it doesn't have to be: a Dagger journey
It's complicated, but it doesn't have to be: a Dagger journeyIt's complicated, but it doesn't have to be: a Dagger journey
It's complicated, but it doesn't have to be: a Dagger journey
Thiago “Fred” Porciúncula
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
Javad Hashemi
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
First Tuesday Bergen
 
Dependency Injection for Android
Dependency Injection for AndroidDependency Injection for Android
Dependency Injection for Android
First Tuesday Bergen
 
Building maintainable app
Building maintainable appBuilding maintainable app
Building maintainable app
Kristijan Jurković
 
Microservices Architecture: Labs
Microservices Architecture: LabsMicroservices Architecture: Labs
Microservices Architecture: Labs
gjuljo
 
Building a Native Camera Access Library - Part II.pdf
Building a Native Camera Access Library - Part II.pdfBuilding a Native Camera Access Library - Part II.pdf
Building a Native Camera Access Library - Part II.pdf
ShaiAlmog1
 
Solid angular
Solid angularSolid angular
Solid angular
Nir Kaufman
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
秀吉(Hsiu-Chi) 蔡(Tsai)
 
Popup view on Mortar
Popup view on MortarPopup view on Mortar
Popup view on Mortar
Keishin Yokomaku
 
Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
Python Ireland
 
Avoiding and dealing with conflicting updates in Oak
Avoiding and dealing with conflicting updates in OakAvoiding and dealing with conflicting updates in Oak
Avoiding and dealing with conflicting updates in Oak
michid
 
Using and contributing to the next Guice
Using and contributing to the next GuiceUsing and contributing to the next Guice
Using and contributing to the next Guice
Adrian Cole
 
JSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan ZarnikovJSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan Zarnikov
Christoph Pickl
 

Similar to Dagger for dummies (20)

guice-servlet
guice-servletguice-servlet
guice-servlet
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
 
Google GIN
Google GINGoogle GIN
Google GIN
 
Testable Android Apps using data binding and MVVM
Testable Android Apps using data binding and MVVMTestable Android Apps using data binding and MVVM
Testable Android Apps using data binding and MVVM
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzg
 
SOLID
SOLIDSOLID
SOLID
 
It's complicated, but it doesn't have to be: a Dagger journey
It's complicated, but it doesn't have to be: a Dagger journeyIt's complicated, but it doesn't have to be: a Dagger journey
It's complicated, but it doesn't have to be: a Dagger journey
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
 
Dependency Injection for Android
Dependency Injection for AndroidDependency Injection for Android
Dependency Injection for Android
 
Building maintainable app
Building maintainable appBuilding maintainable app
Building maintainable app
 
Microservices Architecture: Labs
Microservices Architecture: LabsMicroservices Architecture: Labs
Microservices Architecture: Labs
 
Building a Native Camera Access Library - Part II.pdf
Building a Native Camera Access Library - Part II.pdfBuilding a Native Camera Access Library - Part II.pdf
Building a Native Camera Access Library - Part II.pdf
 
Solid angular
Solid angularSolid angular
Solid angular
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
 
Popup view on Mortar
Popup view on MortarPopup view on Mortar
Popup view on Mortar
 
Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
 
Avoiding and dealing with conflicting updates in Oak
Avoiding and dealing with conflicting updates in OakAvoiding and dealing with conflicting updates in Oak
Avoiding and dealing with conflicting updates in Oak
 
Using and contributing to the next Guice
Using and contributing to the next GuiceUsing and contributing to the next Guice
Using and contributing to the next Guice
 
JSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan ZarnikovJSUG - Google Guice by Jan Zarnikov
JSUG - Google Guice by Jan Zarnikov
 

Recently uploaded

Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 

Recently uploaded (20)

Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 

Dagger for dummies

  • 1. ABUSING DEPENDENCY INJECTION WITH DAGGER FOR DUMMIES Saúl Díaz (@sefford) Freakend ‘15
  • 2. new Flipper(new Gintonic(new Gin(new Alcohol()))) FlipperGintonic GinAlcohol
  • 3. SHOULD I USE DI WITH MY PERSONAL/PRO PROJECT?
  • 6. @Module( includes = RosieModule.class, addsTo = {AndroidModule.class, DrinkModule.class, CodingModule.class } injects = FlipperFragment.class, complete = true, ) public class FlipperModule { @Provides @Named(“Tuenti”) public FlipperInterface provideFlipper(Gintonic drink, Trolling trolling, Android android) { return new FlipperImpl(drink, trolling, android); } }
  • 7. public class FlipperFragment extends Fragment { ObjectGraph graph; @Inject FlipperInterface flipper; @Override public void onCreate(Bundle bundle) { graph = ObjectGraph.create(new FlipperModule()); graph.inject(this); FlipperInterface flipper2 = graph.get(Flipper.class); }
  • 8. IDSPISPOPD FOR SINGLETONS @Singleton ● By definition, Singletons cannot be tested ● Provides same instance of class for Graph ● This instance is not static
  • 10. Lazy<Flipper> lazyFlipper; ● Creates the instance on lazyFlipper.get() ● Returns always the same instance (d’oh) ● Always done in UIThread! LAZY INITIALIZATIONS
  • 11. PROVIDING INSTANCES Provider<Flipper> flipperProvider; ● Can help building your own Flipper army ● Returns a different instance each time
  • 12. @Module( override = true; injects = FlipperFragment.class, complete = true, ) public class FlipperTestModule { @Provides @Singleton public FlipperInterface provideFlipper() { return mock(FlipperInterface.class); } } MOCKING INSTANCES
  • 13. DYNAMIC (SCOPED) INJECTION public ObjectGraph extend() { return graph.plus(new ExtensionModule1(), new ExtensionModule2()); }
  • 14. STATIC INJECTION ● Reduce boilerplate and mantenibility by annotating classes and constructions ● Compilation-validated graph
  • 15. SCOPED INJECTION ● Using a SOLID approach, change implementations on runtime ● Graph runtime, validated ● Risk of “BOOOOOOM”
  • 17. CLASSIC EXAMPLE @Module(injects = LoginActivity.class, addsTo = AppModule.class) // @author Antonio Leiva public class LoginModule { private LoginView view; public LoginModule(LoginView view) { this.view = view; } @Provides @Singleton public LoginView provideView() { return view; } @Provides @Singleton public LoginPresenter providePresenter(LoginView loginView, LoginInteractor loginInteractor) { return new LoginPresenterImpl(loginView, loginInteractor); } }
  • 18. WHY STOPPING THERE?public class NoNfcModule() { @Provides public NfcInterfaceController provideNfcController() { return new NullNfcController(); } } public class NfcModule() { @Provides public NfcInterfaceController provideNfcController( NfcAdapter adapter) { return new NfcController(adapter); } }
  • 19. // This goes on Api15Module @Provides @Singleton public StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI15Controller(); } // This goes on Api19Module @Provides @Singleton public StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI19Controller(); } // This goes on Api21Module @Provides @Singleton public StatusBarControllerInterface provideStatusBarControllerInterface() { return new StatusBarAPI21Controller(); }
  • 20. ONE MORE CUP OF ABUSEpublic class ActiveProfileModule() { @Provides public ProfilePresenterInterface provideProfilePres(...) { return new ActiveProfilePresenter(basePresenter,...); } } public class RegularProfileModule() { @Provides public ProfilePresenterInterface provideProfilePres(...) { return new RegularProfilePresenter(basePresenter,...); } }