1) The document discusses the promises and concepts of Clean Architecture for Android development, including independence from frameworks, testability, separation of concerns, and independence from UI and data.
2) It provides examples of how Clean Architecture maps to Android, with entities, use cases, repositories, presenters, view models, and views separated into layers.
3) It addresses questions around Clean Architecture, such as whether to have one or multiple use cases per screen, how to handle data validation and caching, and whether to use it for minor UI actions. The document advocates for Clean Architecture but notes it requires team investment.
27. UI Thread Worker Thread
Une interprétation de Clean Architecture en action
Core
Interactor
Repository
Interface
Entity
Presenter
Presenter
View
Model
View
Interface
Entity
Repository
Repository
API
Model
Entity
Activity
Fragment
28. UI Thread Worker Thread
Une autre interprétation de Clean Architecture
Interactor
Activity
Fragment
Repository
PresenterView
Controller
Android Java/Kotlin
29. Entity
public class Talk {
private final String title;
private final Date startingDate;
public Talk(String title, Date startingDate) {
this.title = title;
this.startingDate = startingDate;
}
public String getTitle() {
return title;
}
public Date getStartingDate() {
return startingDate;
}
}
30. Interactor
public class TalkInteractor {
private final TalkRepository repository;
private final TalkPresenter presenter;
public void loadTalk(String talkId) {
if (argumentIsValid(talkId)) {
try {
Talk talk = repository.getTalk(talkId);
presenter.presentTalk(talk);
} catch (TalkRepository.TalkRetrievalException e) {
presenter.presentErrorRetrievingTalk();
}
} else {
presenter.presentInvalidArgument();
}
}
}
31. Api Model
@AutoValue
public abstract class JsonTalk {
@JsonCreator
public static JsonTalk newInstance(
@JsonProperty("title") String title,
@JsonProperty("date") Date date) {
return new AutoValue_JsonTalk(title,date);
}
public abstract String getTitle();
public abstract Date getDate();
}
{
"talk": {
"title": "One year of clean architecture",
”date": "2017-04-10T16:20:00"
}
}
32. Repository implementation
public class RetrofitTalkRepository implements TalkRepository {
private final Retrofit retrofit;
@Override
public Talk getTalk(String talkId) throws TalkRetrievalException {
try {
Response<JsonTalk> response = retrofit
.create(Service.class).getTalk(talkId).execute();
if (response.isSuccessful()) {
JsonTalk jsonTalk = response.body();
return transformToEntity(jsonTalk);
}
} catch (IOException ignored) {
}
throw new TalkRetrievalException();
}
}
33. ViewModel
public class TalkViewModel {
private final String title;
private final String startingDate;
public TalkViewModel(String title,String startingDate) {
this.title = title;
this.startingDate = startingDate;
}
public String getTitle() {
return title;
}
public String getStartingDate() {
return startingDate;
}
}
34. Presenter implementation
public class AndroidTalkPresenter implements TalkPresenter {
private final TalkView view;
@Override
public void presentInvalidArgument() {
view.displayError();
}
@Override
public void presentErrorRetrievingTalk() {
view.displayError();
}
@Override
public void presentTalk(Talk talk) {
TalkViewModel viewModel = transformToViewModel(talk);
view.displayTalk(viewModel);
}
}
35. Activity
public class TalkActivity extends AppCompatActivity
implements TalkView {
@Override
public void displayError() {
viewFlipper.setDisplayedChild(DISPLAY_ERROR);
}
@Override
public void displayTalk(TalkViewModel viewModel) {
viewFlipper.setDisplayedChild(DISPLAY_TALK);
titleView.setText(viewModel.getTitle());
startingDateView.setText(viewModel.getStartingDate());
}
}
36. Transformation de la donnée
Repository
"startingDate":
"10-04-2017"
LocalDate
Interactor
LocalDate
Presenter
"Lundi 10
avril 2017"
38. Et alors ?
Use-case first,
Activity last
Frontières
claires
Use-cases
full Java/Kotlin
TDD 4 real
Cohabitation de 2 libs
pour la couche
réseau
39. C’est quoi le hic ?
¤ Single responsability -> many objects
¤ Nécessite l’investissement de toute l’équipe
¤ Schéma nécessaire
¤ Objets passe-plat