SlideShare a Scribd company logo
Keeping it Clean:
Clean code, for dirty developers
- Rich King -
Senior Android Developer
100+ million installs
190 countries
100K+ lines of code
185 Activities
23 Services
Clean Architecture
Architecture
Start of development
ActivitiesLoadersNetwork
Some time passes
Services
ActivitiesLoadersNetwork Global
Cache
And then…..
Services
ActivitiesEvent BusNetwork Global
Cache
GCM
Global
Listener
DB
Clean Architecture
Technical Debt
Time
Debt
So, what is Clean Architecture?
“ A GOOD ARCHITECTURE
EMPHASIZES THE USE-CASES
AND DECOUPLES THEM FROM
PERIPHERAL CONCERNS
— Robert C. Martin
Clean Architecture
• Coined by Robert C. Martin
• Combination of various ideas
- Hexagonal Architecture (a.k.a. Ports and Adapters)
- Onion Architecture
- Screaming Architecture
- Data, context and interaction paradigm
- Boundary, controller entity objects
- Single responsibility principle
https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
Clean Architecture
Key points:
1. Use cases
2.Structure
3.Dependancies
4.Models
5.Testable
Use cases
• Use cases capture business rules
Send
Message
Bob Alice
What does it do?
• Structure should indicate what the application is, not how it
does it.
What does it do?
• Structure should indicate what the application is, not how it
does it.
com.myapp
activities
services
presenters
content providers
views
What does it do?
• Structure should indicate what the application is, not how it
does it.
chat
conversations
com.myapp
Dependencies
UI
Business Logic
Data
Infrastructure
Dependencies
Use Cases
PresentersU
I
H
TTP
D
B
C
ontrollers
Services
Dependencies
Inversion of Control
Class A Class B
Dependency
Inversion of Control
Class A Class B
Injected
Inversion of Control
Send
Message
Injected
Inversion of Control
Send
Message
Carrier
Pigeon
Injected
Inversion of Control
Send
Message
HTTP
Injected
Why different models?
Why different models?
Use Case Model
•message
•id
•from
Data Model
•message
•id
•from
•cacheTime
View Model
•message
•id
•from
•itemSpacing
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
Gateway
Use Case
Entity
Entity
Entity
<I>
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
Presenter
View
Model
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
List of
Messages
Open
Conversation
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Bob
Implementation Details
interface MessageGateway {
List<Message> get(String conversationId);
int send(String conversationId, String to, String from,
String message);
boolean delete(String messageId);
void subscribe(Callback callback);
}
Gateway
class GetMessages {
...
List<M> execute(conversationId) {
return mMessageGateway.get(conversationId);
}
}
Use cases
class SendMessage {
...
int execute(conversationId, ...) {
int result = mMessageGateway.send(...);
if(result == SUCCESS)
mConversationGateway.update(conversationId);
return result;
}
}
Use cases
class SendMessage {
...
int execute(conversationId, ...) {
int result = mMessageGateway.send(...);
if(result == SUCCESS)
mConversationGateway.update(conversationId);
return result;
}
}
Use cases
class SendMessage {
...
int execute(conversationId, ...) {
int result = mMessageGateway.send(...);
if(result == SUCCESS)
mConversationGateway.update(conversationId);
return result;
}
}
Use cases
class MessageScreenPresenter {
...
void onSendMessage(message) {
int result = mSendMessage.execute(...);
switch (result) {
case ERROR:
mView.displayError(...)
...
}
}
Presentation
MainThread
RxJava
• Simple to switch threads
• Transformations are convenient
• Comes with a cost
https://github.com/ReactiveX/RxJava
mExecutor.post(new Runnable() {
void run() {
// do something
mHandler.post(new Runnable() {
void run() {
// publish result
}
});
}
});
!RxJava
mExecutor.post(() -> {
// do something
mHandler.post(() -> {
// publish result
}
}
}
}
!RxJava with lambdas
doSomething()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> /* Handle Result */)
RxJava
Rx, Very Nice
interface MessageGateway {
Single<List<M>> get(...);
Single<Integer> send(...);
Single<Boolean> delete(...);
Observable<Update> subscribe(...);
}
Gateway
class GetMessages {
...
Single<List<M>> execute(conversationId) {
return mMessageRepo.get(conversationId);
}
}
Use cases
class SendMessage {
...
Single<Integer> execute(conversationId, ...) {
return mMessageGateway.send(…)
.doOnNext(r -> if(r == SUCCESS)
mConversationGateway.update(conversationId);)
}
}
Use cases
class MessageScreenPresenter {
...
void onStart() {
mSendMessage.execute(mConversationId)
.observeOn(AndroidSchedulers.mainThread())
.filter(result -> result == ERROR)
.subscribe(mView::displayError);
...
}
}
Presentation
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
Clean Architecture
Was it worth it?
• Boilerplate
• Learning curve
• Testing
• Everything has a place
• Decisions can be postponed
• Implementations are replaceable
How can you do it?
• Start small
• Discuss and experiment
• Tech Talks
• Code reviews
Clean Architecture
Cheers!
github.com/badoo/Chateau
techblog.badoo.com
github.com/kingamajick
@kingamajick

More Related Content

What's hot (20)

PPTX
Clean architecture
andbed
 
PDF
Clean architecture
Lieven Doclo
 
PDF
Domain Driven Design
Young-Ho Cho
 
PDF
Clean Architecture
Flavius Stef
 
PDF
Kata: Hexagonal Architecture / Ports and Adapters
holsky
 
PPT
Spring Boot in Action
Alex Movila
 
PPTX
Domain Driven Design
Ryan Riley
 
PPTX
Domain Driven Design
Araf Karsh Hamid
 
PPTX
Clean Code
Victor Rentea
 
PPT
Domain Driven Design (DDD)
Tom Kocjan
 
PDF
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Steve Pember
 
PPT
SOLID Design Principles
Andreas Enbohm
 
PDF
Design patterns for microservice architecture
The Software House
 
PPTX
Vertical Slicing Architectures
Victor Rentea
 
PDF
Clean Architecture Essentials - Stockholm Software Craftsmanship
Ivan Paulovich
 
PDF
Hexagonal architecture: how, why and when
Xoubaman
 
PPTX
Introduction to spring boot
Santosh Kumar Kar
 
PDF
2012 the clean architecture by Uncle bob
GEORGE LEON
 
PDF
Arquitectura hexagonal
540deg
 
PPTX
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
Clean architecture
andbed
 
Clean architecture
Lieven Doclo
 
Domain Driven Design
Young-Ho Cho
 
Clean Architecture
Flavius Stef
 
Kata: Hexagonal Architecture / Ports and Adapters
holsky
 
Spring Boot in Action
Alex Movila
 
Domain Driven Design
Ryan Riley
 
Domain Driven Design
Araf Karsh Hamid
 
Clean Code
Victor Rentea
 
Domain Driven Design (DDD)
Tom Kocjan
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Steve Pember
 
SOLID Design Principles
Andreas Enbohm
 
Design patterns for microservice architecture
The Software House
 
Vertical Slicing Architectures
Victor Rentea
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Ivan Paulovich
 
Hexagonal architecture: how, why and when
Xoubaman
 
Introduction to spring boot
Santosh Kumar Kar
 
2012 the clean architecture by Uncle bob
GEORGE LEON
 
Arquitectura hexagonal
540deg
 
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 

Viewers also liked (20)

PDF
A Separation of Concerns: Clean Architecture on Android
Outware Mobile
 
PPT
Design pattern in android
Jay Kumarr
 
PPTX
Clean architecture on android
Benjamin Cheng
 
PDF
Android Clean Architecture for Dummies
Kengo Suzuki
 
PPTX
Lightning Talk - Clean Architecture and Design
Deivison Sporteman
 
PDF
Clean architecture: Android
intive
 
PPTX
Clean Architecture by Andrzej Bednarz
NetworkedAssets
 
PPTX
Android Design Principles and Popular Patterns
Faiz Malkani
 
PPTX
Onion Architecture
matthidinger
 
PDF
Is Activity God? ~ The MVP Architecture ~
Ken William
 
PDF
Effective Android UI - English
Pedro Vicente Gómez Sánchez
 
PDF
Android cleanarchitecture
Tomoaki Imai
 
PDF
Skroutz Android MVP and Adapter Delegates presentation
gmetal
 
PPTX
Android Effective UI: Tips, Tricks and Patterns
Adham Enaya
 
PDF
Android Architecture MVP Pattern
Jeff Potter
 
PDF
iOS advanced architecture workshop 3h edition
Jorge Ortiz
 
PDF
Designing a participatory sensing game with children
University of Central Lancashire
 
PDF
Clean architecture - PHP
Michal Giergielewicz
 
PDF
Efficient HTTP Apis
Adrian Cole
 
PDF
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
IKS Gesellschaft für Informations- und Kommunikationssysteme mbH
 
A Separation of Concerns: Clean Architecture on Android
Outware Mobile
 
Design pattern in android
Jay Kumarr
 
Clean architecture on android
Benjamin Cheng
 
Android Clean Architecture for Dummies
Kengo Suzuki
 
Lightning Talk - Clean Architecture and Design
Deivison Sporteman
 
Clean architecture: Android
intive
 
Clean Architecture by Andrzej Bednarz
NetworkedAssets
 
Android Design Principles and Popular Patterns
Faiz Malkani
 
Onion Architecture
matthidinger
 
Is Activity God? ~ The MVP Architecture ~
Ken William
 
Effective Android UI - English
Pedro Vicente Gómez Sánchez
 
Android cleanarchitecture
Tomoaki Imai
 
Skroutz Android MVP and Adapter Delegates presentation
gmetal
 
Android Effective UI: Tips, Tricks and Patterns
Adham Enaya
 
Android Architecture MVP Pattern
Jeff Potter
 
iOS advanced architecture workshop 3h edition
Jorge Ortiz
 
Designing a participatory sensing game with children
University of Central Lancashire
 
Clean architecture - PHP
Michal Giergielewicz
 
Efficient HTTP Apis
Adrian Cole
 
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
IKS Gesellschaft für Informations- und Kommunikationssysteme mbH
 
Ad

Similar to Clean Architecture (20)

PDF
Five android architecture
Tomislav Homan
 
PDF
Android application architecture
Romain Rochegude
 
PDF
Android Clean Architecture with Kotlin and RxJava
Adrián Santalla Romero de Ávila
 
PDF
Clean Architecture
Ayush Maharjan
 
PDF
Nicholas Gustilo "Clean Android: building great mobile apps"
IT Event
 
PDF
Building Scalable Stateless Applications with RxJava
Rick Warren
 
PDF
Clean Architecture @ Taxibeat
Michael Bakogiannis
 
PPTX
Architecting modern Android apps
Grigori Hlopkov
 
PDF
Jorge D. Ortiz Fuentes "Hands on Implementation of Clean Architecture for And...
IT Event
 
PDF
Android clean architecture workshop 3h edition
Jorge Ortiz
 
PDF
Clean Architecture in Android. UPTech TechTalk
Halyna Halkina
 
PDF
Lublin Startup Festival - Mobile Architecture Design Patterns
Karol Szmaj
 
PDF
Composable Software Architecture with Spring
Sam Brannen
 
PDF
Modern Android app library stack
Tomáš Kypta
 
PDF
My way to clean android (EN) - Android day salamanca edition
Christian Panadero
 
PPTX
Lecture android best practices
eleksdev
 
PDF
Олексій Пастухов, "Чи є життя без single-state management?"
Sigma Software
 
PPTX
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
PDF
Adapting clean architecture in android apps
Matso Abgaryan
 
Five android architecture
Tomislav Homan
 
Android application architecture
Romain Rochegude
 
Android Clean Architecture with Kotlin and RxJava
Adrián Santalla Romero de Ávila
 
Clean Architecture
Ayush Maharjan
 
Nicholas Gustilo "Clean Android: building great mobile apps"
IT Event
 
Building Scalable Stateless Applications with RxJava
Rick Warren
 
Clean Architecture @ Taxibeat
Michael Bakogiannis
 
Architecting modern Android apps
Grigori Hlopkov
 
Jorge D. Ortiz Fuentes "Hands on Implementation of Clean Architecture for And...
IT Event
 
Android clean architecture workshop 3h edition
Jorge Ortiz
 
Clean Architecture in Android. UPTech TechTalk
Halyna Halkina
 
Lublin Startup Festival - Mobile Architecture Design Patterns
Karol Szmaj
 
Composable Software Architecture with Spring
Sam Brannen
 
Modern Android app library stack
Tomáš Kypta
 
My way to clean android (EN) - Android day salamanca edition
Christian Panadero
 
Lecture android best practices
eleksdev
 
Олексій Пастухов, "Чи є життя без single-state management?"
Sigma Software
 
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Adapting clean architecture in android apps
Matso Abgaryan
 
Ad

More from Badoo (6)

PDF
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
Badoo
 
PDF
Mobile Web Test Automation: to the Desktop! - Alexander Bayandin - Mobile Tes...
Badoo
 
PDF
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
Badoo
 
PDF
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Badoo
 
PDF
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Badoo
 
PDF
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
Badoo
 
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
Badoo
 
Mobile Web Test Automation: to the Desktop! - Alexander Bayandin - Mobile Tes...
Badoo
 
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
Badoo
 
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Badoo
 
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Badoo
 
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
Badoo
 

Clean Architecture