43
2015/11/17 @kengoScal Android Clean Architecture for Dummies

Android Clean Architecture for Dummies

Embed Size (px)

Citation preview

Page 1: Android Clean Architecture for Dummies

2015/11/17 @kengoScal

Android Clean Architecture for

Dummies

Page 2: Android Clean Architecture for Dummies

About Me

2

Page 3: Android Clean Architecture for Dummies

Self Introductionname: Kengo Suzuki twitter: @kengoScal

3

2011~2014:Security Analyst 2014年11月: Joined MoneyForward 2014年11月~2015年01月: iOS 2015年02月~08月:Android 2015年09月~10月 : Security Architect 2015年11月 : Security Architect/Management + Android

Page 4: Android Clean Architecture for Dummies

4

In Charge of New Android App!

Page 5: Android Clean Architecture for Dummies

Android Clean Architecture

5

Page 6: Android Clean Architecture for Dummies

What is it & Why using it?

6

Page 7: Android Clean Architecture for Dummies

What is it & Why using it?

7

Not Today.

Page 8: Android Clean Architecture for Dummies

Plenty of Good Entries(with source codes)

8

• English • Architecting Android…The Clean Way? • Clean Android Architecture • MVP for Android: how to organize presentation layer

• Japanese • AndroidオールスターズでClean Architectureについて発表してきた

• これからの設計の話をしよう • AndroidではMVCよりMVPの方がいいかもしれない

Page 9: Android Clean Architecture for Dummies

Plenty of Good Entries(with source codes)

9

• English • Architecting Android…The Clean Way? • Clean Android Architecture • MVP for Android: how to organize presentation layer

• Japanese • AndroidオールスターズでClean Architectureについて発表してきた

• これからの設計の話をしよう • AndroidではMVCよりMVPの方がいいかもしれない

Big Picture

Source Code

Roles

Big Picture

Roles

Presenter

Page 10: Android Clean Architecture for Dummies

Right off the Bat?

10

訳: すぐに/直ちに

Page 11: Android Clean Architecture for Dummies

Need Endurance!

11

• Codes aren’t self explanatory • Lack of experience in using core liibraries could be pain

• Don’t know what goes where.

Page 12: Android Clean Architecture for Dummies

12

Page 13: Android Clean Architecture for Dummies

“Architecure is About Intent, not Frameworks”

13

by Robert Cecil Martin

Page 14: Android Clean Architecture for Dummies

With the Hope, no one gives up architecting

14

Page 15: Android Clean Architecture for Dummies

How to use it (Tutorial)

15

Page 16: Android Clean Architecture for Dummies

Primitive App

16

• Single Activity • Single List View

• Horizontally aligned texts • Fab

• click and fetch data • Fetch MusicList(data)

• by Retrofit • freemusicarchive.org

• DI and EventHub • Under Construction

Page 17: Android Clean Architecture for Dummies

Click And Fetch Data Tutorial

17

Page 18: Android Clean Architecture for Dummies

The Goal

18

• Click Fab • Run Retrofit to fetch data • Not considering data response

Page 19: Android Clean Architecture for Dummies

19

What We Will Implement

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 20: Android Clean Architecture for Dummies

Thing You Need To Implement

20

• MainActivity • MusicListPresenter • MusicListUseCase • MusicListUseCaseImpl • MusicListRepository • MusicListRepositoryImpl • MusicListEntity

Page 21: Android Clean Architecture for Dummies

Data

Domain

Presentation

Thing You Need To Implement

21

• MainActivity • MusicListAdapter • MusicListPresenter • MusicListUseCase • MusicListUseCaseImpl • MusicListRepository • MusicListRepositoryImpl • MusicListEntity

Page 22: Android Clean Architecture for Dummies

Presentation - UserAction

22

• Main Activity & MusicListAdapter • Render List View attaching adapter • Initialize followings

• repository, usecase, presenter • Pass FAB clicked event along with Main Thread Info to Presenter

• MusicListPresenter • Receive FAB clicked event from View

• Execute MusicListUseCase(Imp) Job • Pass thread info to MusicListUseCas(Imp)

Page 23: Android Clean Architecture for Dummies

23

What We Just Implemented

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 24: Android Clean Architecture for Dummies

Domain - User Action to Data Interface

24

• MusicUseCase • Provide interface between Domain Layer & Presentation Layer

• MusicUseCaseImp • Ask MusicListRepository(Imp) for API(External Resource)

• MusicListRepository • Provide interface between Domain Layer & Data Layer

Page 25: Android Clean Architecture for Dummies

25

What We Just Implemented

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 26: Android Clean Architecture for Dummies

Data - Data Interface to DataSource

26

• MusicListRepositoryImp • To Fetch external Data • Declare and Execute Retrofit Http Client

• GET http://freemusicarchive.org/api?key={key}

• MusicListEntity • Parse Response and map to domain/model

• gson will handle it.

Page 27: Android Clean Architecture for Dummies

27

What We Just Implemented

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 28: Android Clean Architecture for Dummies

That’s it for Fetching Data

28

Now we want to Reflect Data

Page 29: Android Clean Architecture for Dummies

Reflect Data to View Tutorial

29

Page 30: Android Clean Architecture for Dummies

The Goal

30

• Pass back data just fetched • Update View

Page 31: Android Clean Architecture for Dummies

31

What We Will Implement

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 32: Android Clean Architecture for Dummies

Thing You Need To Implement

32

• MusicListRepository#MusicListFetchCallback • MusicListUseCaseImpl • MusicListUseCase#MusicListUseCaseCallback • MusicListPresenter • MusicListPresenter#MusicListViewCallback • MainActivity

Page 33: Android Clean Architecture for Dummies

Presentation

DomainData

Thing You Need To Implement

33

• MusicListRepository#MusicListFetchCallback • MusicListUseCaseImpl • MusicListUseCase#MusicListUseCaseCallback • MusicListPresenter • MusicListPresenter#MusicListViewCallback • MainActivity

Page 34: Android Clean Architecture for Dummies

Domain - Passing Data to User Interface

34

• MusicListRepository#MusicListFetchCallback • MusicListUseCaseImpl

• Implement MusicListFetchCallback • So that Main Threat can receive runnable with command with fetched data

• MusicListUseCase#MusicListUseCaseCallback • MusicListPresenter • MusicListPresenter#MusicListViewCallback • MainActivity

Page 35: Android Clean Architecture for Dummies

35

What We Just Implemented

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 36: Android Clean Architecture for Dummies

Presentation - Updating View

36

• MusicListUseCase#MusicListUseCaseCallback • MusicListPresenter#MusicListViewCallback • MusicListPresenter

• Implement MusicListUseCaseCallback • Execute methods in MusicListViewCallback

• MainActivity • Implement MusicListViewCallback

• So that User Case can pass fetched data • So that Activity can update adapter data

Page 37: Android Clean Architecture for Dummies

37

What We Just Implemented

• reference • Architecting Android…The Clean Way?: • これからの設計の話をしよう

Page 38: Android Clean Architecture for Dummies

That’s it!

38

Page 39: Android Clean Architecture for Dummies

Lots of Room for Improvement

39

• Callback Repetition is quite Ugly • Many Reinvent Wheel

• Not using Dagger, RxJava • Not a big fun of passing thread around • Tutorial itself wasn’t thoroughly covered

Page 40: Android Clean Architecture for Dummies

Still, Worthwhile

40

Especially, in the team

Page 41: Android Clean Architecture for Dummies

Enjoy Clean Architecting!

41

Page 42: Android Clean Architecture for Dummies

Thank you!

Page 43: Android Clean Architecture for Dummies

References

43

• English • Architecting Android…The Clean Way?: http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/

• Clean Android Architecture: https://speakerdeck.com/richk/clean-android-architecture

• MVP for Android: how to organize presentation layer: http://antonioleiva.com/mvp-android/

• Japanese • AndroidオールスターズでClean Architectureについて発表してきた: http://tomoima525.hatenablog.com/entry/2015/08/13/190731

• これからの設計の話をしよう: http://www.slideshare.net/shinnosukekugimiya/ss-50705888

• AndroidではMVCよりMVPの方がいいかもしれない: http://konifar.hatenablog.com/entry/2015/04/17/010606