SlideShare a Scribd company logo
1 of 85
Download to read offline
A Separation of Concerns
Kamal Kamal Mohamed
Android Developer, //TODO Find Better Title @ Outware Mobile
Ryan Hodgman
Official Despiser of Utils Classes @ Outware Mobile
Clean Architecture on Android
18/09/2015 - YOW Connected 2015
Why are we here?
To share with you our journey in
applying Clean Architecture to an
Android project
...and get some valuable feedback
Premise
Our own interpretation
Under development
Not a silver bullet
Iterating on it on a day to day basis
Best suited for medium-big projects
Multiple possible implementations
What are our goals as developers?
Easy to refactor on volatile projects
Maintainable
What are our goals as developers?
Easy to refactor on volatile projects
Maintainable
Be confident that the code is reliable
Testable
What are our goals as developers?
Easy to refactor on volatile projects
Maintainable
Be confident that the code is reliable
Testable
Can increase scope without requiring a rework
Scalable
What are our goals as developers?
Easy to refactor on volatile projects
Maintainable
Be confident that the code is reliable
Testable
Reduce the difficulty of future development efforts
Readable
Can increase scope without requiring a rework
Scalable
Introduction to Clean
What is Clean Architecture?
Robert Martin’s original depiction of Clean Architecture https://blog.
8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
The Dependency Rule
Source code dependencies can only point inwards
How many layers?
(The answer is three)
Separation of Concerns
Presentation Logic
Defines how to display visual information to the user
Examples:
● Organize screen layout
● Format text
● Enable user input
Separation of Concerns
Business Logic
Implements the business requirements of the application
Examples:
● Verify user authorization
● Choose data to be displayed
● Determine if additional user input is required
Separation of Concerns
Data Logic
Stores and retrieves data to achieve the business requirements
Examples:
● Make an API call
● Query a local database
● Access the local filesystem
Architectural Diagram
Image Source: Lucasfilm Ltd. All Rights Reserved
Architectural Diagram
Domain Layer
Domain Layer
Entity
Represents a business object that concerns the application
Least likely to change when shifting requirements
Responsibilities
Entity
Represents a business object that concerns the application
Least likely to change when shifting requirements
● Model the relationships with real-world
objects / concepts
● Encapsulate the properties required by
the application’s business logic
Responsibilities Interactions
Entity
Represents a business object that concerns the application
Least likely to change when shifting requirements
● Model the relationships with real-world
objects / concepts
● Encapsulate the properties required by
the application’s business logic
● May be used directly by all layers of the
application
● Likely that they may be mapped from
data sources or to presentable view
models
Entity
Entity
Properties
Entity
Entity-specific logic
Use Case
Contains the business logic related to a specific use case
Can be run and canceled
Responsibilities
Use Case
Contains the business logic related to a specific use case
Can be run and canceled
● Execute a piece of business logic
● Define a Callback to provide results
● Inform the Callback of the result
● Interact with the data layer
Responsibilities Interactions
Use Case
Contains the business logic related to a specific use case
Can be run and canceled
● Execute a piece of business logic
● Define a Callback to provide results
● Inform the Callback of the result
● Interact with the data layer
Use Case
Use Case
Callback
Repository
Callback
Notifies
Uses
Repository
Implements
Use Case
Use Case
One or more Repository objects
Use Case
May be run and cancelled
Use Case
Contains business logic
Use Case
Defines interactions
Repository
Asynchronous interface to abstract data access to a specific set of data from
the data layer
Responsibilities
Repository
Asynchronous interface to abstract data access to a specific set of data from
the data layer
● Provide methods to store and retrieve
data of a specific type
● Define Callback interfaces to return the
results of each operation
Responsibilities Interactions
Repository
Asynchronous interface to abstract data access to a specific set of data from
the data layer
● Provide methods to store and retrieve
data of a specific type
● Define Callback interfaces to return the
results of each operation
Repository
Data
Manager
Use Case
ImplementsUses
Repository
Repository
Asynchronous
Repository
Callback definition
A Brief Note on Test-Driven-Development
Presentation Layer
Presentation Layer
Model-View-Presenter
Presenter
View Surface Activity
Presentation
Domain
Fragment
View
Implements
Uses
Uses
Uses
Presenter
Implements presentation logic by directing UI changes and handling user
input
Responsibilities
Presenter
Implements presentation logic by directing UI changes and handling user
input
● Direct UI changes
● Handle user triggered UI events
● Create and trigger Use Case objects
● Define a View Surface interface
Responsibilities Interactions
Presenter
Implements presentation logic by directing UI changes and handling user
input
● Direct UI changes
● Handle user triggered UI events
● Create and trigger Use Case objects
● Define a View Surface interface
Presenter
View Surface
Use Case
Callback
Uses
Triggers
Use Case
Implements
Presenter
Presenter
Systems Surfaces and Use Cases
Presenter
Lifecycle events
Presenter
UI event management
View Surface
Abstracts away the implementation of the UI logic, and is typically implemented
by a View, Activity or Fragment
Responsibilities
View Surface
Abstracts away the implementation of the UI logic, and is typically implemented
by a View, Activity or Fragment
● Screen layout
● Animations / transitions
● Propagate UI events to the Presenter
● Navigation
Responsibilities Interactions
View Surface
Abstracts away the implementation of the UI logic, and is typically implemented
by a View, Activity or Fragment
● Screen layout
● Animations / transitions
● Propagate UI events to the Presenter
● Navigation
Activity
Presenter
Presenter
View Surface
InformsImplements
View Surface
View Surface
Navigation
View Surface
UI information
View Surface
UI state
System Surface
Abstracts away the implementation of various system functionalities
Responsibilities
System Surface
Abstracts away the implementation of various system functionalities
● Provide functionality specific to the OS /
device
● Gracefully fail in the event of missing
functionality
Responsibilities Interactions
System Surface
Abstracts away the implementation of various system functionalities
● Provide functionality specific to the OS /
device
● Gracefully fail in the event of missing
functionality
System
Surface
Used by
Presenter
System Surface
System Surface
Android system dependencies
System Surface
System-specific implementation
Data Layer
Data Layer
Data Manager
Implements a single Repository interface to handle all the data sources
required for a specific set of data
Responsibilities
Data Manager
Implements a single Repository interface to handle all the data sources
required for a specific set of data
● Provide access to a specific set of data
● Make request on a data source in the
form of a Client
● Handle a cache when required
Responsibilities Interactions
Data Manager
Implements a single Repository interface to handle all the data sources
required for a specific set of data
● Provide access to a specific set of data
● Make request on a data source in the
form of a Client
● Handle a cache when required
Data
Manager
Repository
Callback
Repository
NotifiesImplements
Client
Uses
Client
Encapsulates a particular source of data and exposes an abstracted interface to
the Data Managers
Responsibilities
Client
Encapsulates a particular source of data and exposes an abstracted interface to
the Data Managers
● Make Database queries
● Manage an API connection
● Access Local Storage
● Map data results to domain Entities
Responsibilities Interactions
Client
Encapsulates a particular source of data and exposes an abstracted interface to
the Data Managers
● Make Database queries
● Manage an API connection
● Access Local Storage
● Map data results to domain Entities
Client
Data
Manager
Used by
Mapper
Uses
Handles
Data
Source
Mapper
Converts a data object (API or DB response) to a domain Entity
Responsibilities
Mapper
Converts a data object (API or DB response) to a domain Entity
● Transform data into domain-convenient
Entities
Responsibilities Interactions
Mapper
Converts a data object (API or DB response) to a domain Entity
● Transform data into domain-convenient
Entities
Mapper
Data Object
Entity
Conclusions
Pros
Workflow improvements
Code readability
Testability
No more 1000+ line Activity classes
Isolated business logic
Possibility to work on separate layers in parallel
Cons
Hierarchy implementation
Developer onboarding
Interfaces
Increased ramp-up time for new developers
Interfaces, interfaces, ...
Implementing a feature requires working across all of the layers
Further thoughts
Dagger 2! Can help a lot with keeping the layers clean
Dependency injection
Further thoughts
Dagger 2! Can help a lot with keeping the layers clean
Dependency injection
Where should we split tasks onto a worker thread?
Thread management
Further thoughts
Dagger 2! Can help a lot with keeping the layers clean
Dependency injection
Where should we split tasks onto a worker thread?
Thread management
RxJava! Helps to reduce callbacks and make flows clearer
Functional programming
Further thoughts
Dagger 2! Can help a lot with keeping the layers clean
Dependency injection
Where should we split tasks onto a worker thread?
Thread management
RxJava! Helps to reduce callbacks and make flows clearer
Functional programming
May be useful to model a stateful user flow through the application
User narratives
Next thing you can do!
Consider the separation between data, domain, and
presentation logic in one of your projects
References
The Clean Architecture [Uncle Bob]
https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
Architecting Android…The clean way? [Fernando Cejas]
http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/
Sample Project [Ryan and Kamal]
https://github.com/outware-mobile/android-clean-architecture
Contact details
Kamal Kamal Mohamed
kamal.kamalmohamed@outware.com.au
Ryan Hodgman
ryan.hodgman@outware.com.au

More Related Content

What's hot

Clean architecture
Clean architectureClean architecture
Clean architecture
andbed
 

What's hot (20)

Implementing DDD with C#
Implementing DDD with C#Implementing DDD with C#
Implementing DDD with C#
 
CQRS recipes or how to cook your architecture
CQRS recipes or how to cook your architectureCQRS recipes or how to cook your architecture
CQRS recipes or how to cook your architecture
 
Modelling a complex domain with Domain-Driven Design
Modelling a complex domain with Domain-Driven DesignModelling a complex domain with Domain-Driven Design
Modelling a complex domain with Domain-Driven Design
 
Domain Driven Design: Zero to Hero
Domain Driven Design: Zero to HeroDomain Driven Design: Zero to Hero
Domain Driven Design: Zero to Hero
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Brownfield Domain Driven Design
Brownfield Domain Driven DesignBrownfield Domain Driven Design
Brownfield Domain Driven Design
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Designing APIs and Microservices Using Domain-Driven Design
Designing APIs and Microservices Using Domain-Driven DesignDesigning APIs and Microservices Using Domain-Driven Design
Designing APIs and Microservices Using Domain-Driven Design
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Domain Driven Design(DDD) Presentation
Domain Driven Design(DDD) PresentationDomain Driven Design(DDD) Presentation
Domain Driven Design(DDD) Presentation
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
No Onions, No Tiers - An Introduction to Vertical Slice Architecture by Bill ...
No Onions, No Tiers - An Introduction to Vertical Slice Architecture by Bill ...No Onions, No Tiers - An Introduction to Vertical Slice Architecture by Bill ...
No Onions, No Tiers - An Introduction to Vertical Slice Architecture by Bill ...
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Domain Driven Design
Domain Driven Design Domain Driven Design
Domain Driven Design
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net core
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 

Viewers also liked

Lightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignLightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and Design
Deivison Sporteman
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
Jay Kumarr
 
Onion Architecture
Onion ArchitectureOnion Architecture
Onion Architecture
matthidinger
 
Clean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej BednarzClean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej Bednarz
NetworkedAssets
 

Viewers also liked (20)

Android Clean Architecture for Dummies
Android Clean Architecture for DummiesAndroid Clean Architecture for Dummies
Android Clean Architecture for Dummies
 
Clean architecture on android
Clean architecture on androidClean architecture on android
Clean architecture on android
 
Android cleanarchitecture
Android cleanarchitectureAndroid cleanarchitecture
Android cleanarchitecture
 
Karumi Dojo: Kata Maxibon
Karumi Dojo: Kata MaxibonKarumi Dojo: Kata Maxibon
Karumi Dojo: Kata Maxibon
 
Lightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignLightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and Design
 
World-Class Testing Development Pipeline for Android
 World-Class Testing Development Pipeline for Android World-Class Testing Development Pipeline for Android
World-Class Testing Development Pipeline for Android
 
Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
 
Effective Android UI - English
Effective Android UI - EnglishEffective Android UI - English
Effective Android UI - English
 
Karumi Dojo - String Calculator Kata
Karumi Dojo - String Calculator KataKarumi Dojo - String Calculator Kata
Karumi Dojo - String Calculator Kata
 
Kata Contacts
Kata ContactsKata Contacts
Kata Contacts
 
Onion Architecture
Onion ArchitectureOnion Architecture
Onion Architecture
 
Android DevConference - Android Clean Architecture
Android DevConference - Android Clean ArchitectureAndroid DevConference - Android Clean Architecture
Android DevConference - Android Clean Architecture
 
Dependency injection on Android
Dependency injection on AndroidDependency injection on Android
Dependency injection on Android
 
Android development
Android developmentAndroid development
Android development
 
Clean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej BednarzClean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej Bednarz
 
Android DevConference - Refactoring for RxJava
Android DevConference - Refactoring for RxJavaAndroid DevConference - Refactoring for RxJava
Android DevConference - Refactoring for RxJava
 
Designing a participatory sensing game with children
Designing a participatory sensing game with childrenDesigning a participatory sensing game with children
Designing a participatory sensing game with children
 
The Good Developer - Spanish
The Good Developer - SpanishThe Good Developer - Spanish
The Good Developer - Spanish
 
Moxy – реализация MVP под Android. С щепоткой магии
Moxy – реализация MVP под Android. С щепоткой магииMoxy – реализация MVP под Android. С щепоткой магии
Moxy – реализация MVP под Android. С щепоткой магии
 

Similar to A Separation of Concerns: Clean Architecture on Android

Similar to A Separation of Concerns: Clean Architecture on Android (20)

Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
Dicoding Developer Coaching #31: Android | Menerapkan Clean Architecture di A...
 
A CQRS Journey
A CQRS JourneyA CQRS Journey
A CQRS Journey
 
RedisConf17 - Dynomite - Making Non-distributed Databases Distributed
RedisConf17 - Dynomite - Making Non-distributed Databases DistributedRedisConf17 - Dynomite - Making Non-distributed Databases Distributed
RedisConf17 - Dynomite - Making Non-distributed Databases Distributed
 
Crafted Design - ITAKE 2014
Crafted Design - ITAKE 2014Crafted Design - ITAKE 2014
Crafted Design - ITAKE 2014
 
Deconstructing Monoliths with Domain Driven Design
Deconstructing Monoliths with Domain Driven DesignDeconstructing Monoliths with Domain Driven Design
Deconstructing Monoliths with Domain Driven Design
 
Crafted Design - Sandro Mancuso
Crafted Design - Sandro MancusoCrafted Design - Sandro Mancuso
Crafted Design - Sandro Mancuso
 
Twelve Factor - Designing for Change
Twelve Factor - Designing for ChangeTwelve Factor - Designing for Change
Twelve Factor - Designing for Change
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Dynomite @ RedisConf 2017
Dynomite @ RedisConf 2017Dynomite @ RedisConf 2017
Dynomite @ RedisConf 2017
 
Effects, Coeffects & Subscriptions: a pit of success for SPAs
Effects, Coeffects & Subscriptions: a pit of success for SPAsEffects, Coeffects & Subscriptions: a pit of success for SPAs
Effects, Coeffects & Subscriptions: a pit of success for SPAs
 
Shaping serverless architecture with domain driven design patterns
Shaping serverless architecture with domain driven design patternsShaping serverless architecture with domain driven design patterns
Shaping serverless architecture with domain driven design patterns
 
Shaping serverless architecture with domain driven design patterns
Shaping serverless architecture with domain driven design patternsShaping serverless architecture with domain driven design patterns
Shaping serverless architecture with domain driven design patterns
 
Android meetup
Android meetupAndroid meetup
Android meetup
 
Five android architecture
Five android architectureFive android architecture
Five android architecture
 
Crafted Design - LJC World Tour Mash Up 2014
Crafted Design - LJC World Tour Mash Up 2014Crafted Design - LJC World Tour Mash Up 2014
Crafted Design - LJC World Tour Mash Up 2014
 
Framework Engineering
Framework EngineeringFramework Engineering
Framework Engineering
 
Scaling mobile dev teams
Scaling mobile dev teams Scaling mobile dev teams
Scaling mobile dev teams
 
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
 
Evolutionary Design Solid
Evolutionary Design SolidEvolutionary Design Solid
Evolutionary Design Solid
 
Software Architecture - All you need to know
Software Architecture - All you need to knowSoftware Architecture - All you need to know
Software Architecture - All you need to know
 

Recently uploaded

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (6)

Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 

A Separation of Concerns: Clean Architecture on Android