The document discusses modern app architecture using Dagger 2 for dependency injection and RxJava for asynchronous data flows. It covers dependency injection and how Dagger 2 avoids reflection for faster performance. It also discusses the model-view-presenter pattern to separate business logic from display logic. Finally, it explains how RxJava uses observables and observers to implement asynchronous data streams and how this avoids callback hell and allows pushing data instead of polling.
7. Dependency Injection
• Your class get’s what it needs (Dependenciescome
to you)
• You have no idea where they come from
• A well known pattern
• Everyone uses it (more or less)
App Architektur
Dependency Injection
8. Dependency Injection
• It’s common sense on the server side
• There are already frameworks out there:
–Spring DI
–Google Guice
–Pico Container
–...
App Architektur
Dependency Injection
9. Dependency Injection - Android
Now what about android?
• Mobile (limited ressources)
• App Startup time does really matter
• Reflection is slow
• App Size + method count does really matter
App Architektur
Dependency Injection
10. Dagger 2
Why another Framework?
• no reflection at all
• Based on Dagger1
• Proposed + Implemented by the Java Core Team (Google)
• Code generation (As if you would write it)
App Architektur
Dependency Injection
11. Dagger 2 - Basics
@Modules / @Provides
• Provide Dependenciesto
@Inject
• Requests for Dependencies
@Components
• Public API, the Bridge between Modules and Injects
and some more...
App Architektur
Dependency Injection
17. @Module
public class MockNetworkingModule extends NetworkingModule {
@Provides
@Singleton
TwitterTimeline provideTwitterTimeline() {
return new MockTwitterTimeline();;
}
}
App Architektur
Dependency Injection
18. Dagger2
• DI is a good thing to have
• steep learning curve
• Sometimes hard to get hold on component
references
• Still under active development
• Enforces structured code
• Code generation is a good thing
App Architektur
Dependency Injection
19. Model View Presenter (MViP)
• Seperate Business Logic, Application Logic, Display
Logic
• Change independent parts without Issues
• Easier to Test
–Mock Network Modules etc.
App Architektur
MViP
20. MViP: Components
• Model
–Data that is represented
–for example Tweets
• View
–The View Displaying the Data
–can be Activity, Fragment, CustomView
• Presenter
–Logic
–for example fetching Tweets from Network
App Architektur
MViP
24. MViP: Model
• The Data that is displayed to the User
• Data used to do Interactions with the Presenter
• Can be the same as the Models for Network
Requests
–Retrofit
App Architektur
MViP
25. public class Tweet {
@JsonProperty("created_at")
private String DateCreated;;
@JsonProperty("id")
private long Id;;
@JsonProperty("text")
private String Text;;
@JsonProperty("user")
private TwitterUser User;;
}
App Architektur
MViP -‐ Model
26. • Interface
• Actual Display Unit Implements Interface
–Fragment, Activity, Custom View, whatever…
• Views should be dumb!
–Only react to Actions
• No Business Logic in here
MVP: View
App Architektur
MViP
42. Interactor?
• Do not bloat up Presenters!
• Interactors are used for doing things
–Fetching Stuff from the network
–Fetching Stuff from Disk
• Interface again!
App Architektur
MViP -‐ interactor
43. public interface TwitterTimelineInteractor {
void loadUserTweets(Observer<? super List<Tweet>>
observer, String username);;
void loadHashtagTweets(Observer<? super List<Tweet>>
observer, String hastag);;
Observable<List<Tweet>> loadHashtagTweets(String
hashtag);;
void pollHashtagTweets(Observer<? super List<Tweet>>
observer, String hashtag);;
}
App Architektur
MViP -‐ interactor
44. public class TwitterTimelineInteractorImpl
implements TwitterTimelineInteractor {
TwitterTimeline twitterTimeline;;
@Override
public void loadUserTweets(Observer<? super List<Tweet>>
observer, String username) {
twitterTimeline
.getUserTimeline(username)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);;
}
App Architektur
MViP -‐ interactor
46. Why?
• Push don’t Poll!
–do not stress the UI
–do not stress the Application
• Avoid Lifecycle Problems
• Avoid the Callback Hell
• Death to:
–Boilerplate Code
–AsyncTasks, Timertasks, Loaders, etc...
App Architektur
RXJava
47. RX: What?
• Programming Paradigm based on asynchronous
Data Flow
• Idea from the late 90s
• Erik Meijer (Microsoft) developed first RX extension
for .NET
• Adapted by many Programming languages
App Architektur
RXJava
48. RX: What?
• Ported to JVM by Ben Christensen and Jafar Husain
(Netflix)
–Java, Closure, Groovy, Scala, etc etc….
• RXJava -> RXAndroid
• The Observer pattern done right.
App Architektur
RXJava
http://reactivex.io/
52. Observers
Lifecycle:
–onNext(T):
• gets called when new Data is emited with the
Data
–onCompleted():
• gets called when the Sequence is done
–onError(Throwable):
• gets called when an Error occured
App Architektur
RXJava
53. Observables
• Sequence that emits data
–can emit more than once
• Lives as long as there is work to do
• Observers subscribe to an Observable
• When there is no Subscriber, the Observable emits
the data nowhere
App Architektur
RXJava
58. Transforming the Data
• We do not want a Long as result, we want a String
• map()
–gets the Long value
–returns a String value
• Chainable!
–map().map().map().map().............map()
App Architektur
RXJava
62. Observable<String> interval = Observable
.interval(1, TimeUnit.SECONDS)
.map(new Func1<Long, String>() {
@Override
public String call(Long aLong) {
return "Tick: " + aLong.toString();;
}
});;
Takes the Long
Transforms the Data
and returns a String
App Architektur
RXJava
63. Hello Again Boilerplatecode
• new Func1<T, T>() { public T call(t t2) { … } for every
mapping will turn the code really unreadable and
bloated
• Lambda to the Rescue!
–retrolambda for android & gradle
–https://github.com/evant/gradle-retrolambda
App Architektur
RXJava
73. Example: Livesearch
• Retrieve the user Input from a EditText when text
changes
–only after nothing changes for 600 ms
• Add # infront of the input to do a hastag search
• Call the network (via Retrofit)
–returns StatusesResponse Object
• Transform to List<Tweets>
• Show Tweets in view
App Architektur
RXJava