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
ReadableCan 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 LogicDefines how to display visual information to the user
Examples:
● Organize screen layout
● Format text
● Enable user input
Separation of Concerns
Business LogicImplements 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 LogicStores 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 applicationLeast likely to change when shifting requirements
Responsibilities
Entity
Represents a business object that concerns the applicationLeast 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 applicationLeast 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 caseCan be run and canceled
Responsibilities
Use Case
Contains the business logic related to a specific use caseCan 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 caseCan 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
NotifiesUses
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 ManagerUse 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 SurfaceUse Case Callback
UsesTriggers
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
PresenterPresenterView 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
Android System Dependencies
Abstracted System Specifics
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 CallbackRepository
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
Ryan Hodgman