Presented at YOW! Connected 2015 by Kamal Kamal Mohamed & Ryan Hodgman
As an Android developer, I want to deliver features without making compromises on code quality.
Scenario 1 - Given I am dealing with 1000+ line activities, When I have to develop a complicated feature, Then I waste time orienting myself and fixing bugs.
Scenario 2 - Given I have integrated a backend API directly into my app logic, When that API changes, Then I have to refactor large segments of unrelated logic in order to utilise the new API.
Scenario 3 - Given I have cleanly architected my application, When business/presentation/backend logic changes, Then I can easily update the relevant code without breaking unrelated features!
In this talk, two Android developers will present their take on what a cleanly architected app looks like and why it makes our lives easier. A well-defined separation of concerns has benefits not just for our sanity as developers, but also for the project workflow as it allows multiple developers to collaborate on a single feature with ease. We will be exploring how the domain-driven approach can improve code clarity, allow you to easily write tests, and provide a scalable infrastructure for you to quickly iterate on. Join us on our path of discovery as we discuss the advantages, drawbacks and implementation specifics in the context of a small sample project.
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
Separation of Concerns in Clean Architecture on Android
1. 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
2. Why are we here?
To share with you our journey in
applying Clean Architecture to an
Android project
...and get some valuable feedback
3. 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
4.
5. What are our goals as developers?
Easy to refactor on volatile projects
Maintainable
6. What are our goals as developers?
Easy to refactor on volatile projects
Maintainable
Be confident that the code is reliable
Testable
7. 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
8. 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
14. Separation of Concerns
Presentation Logic
Defines how to display visual information to the user
Examples:
● Organize screen layout
● Format text
● Enable user input
15. 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
16. 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
21. Entity
Represents a business object that concerns the application
Least likely to change when shifting requirements
22. 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
23. 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
27. Use Case
Contains the business logic related to a specific use case
Can be run and canceled
28. 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
29. 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
36. 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
37. 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
46. 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
47. 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
52. View Surface
Abstracts away the implementation of the UI logic, and is typically implemented
by a View, Activity or Fragment
53. 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
54. 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
60. 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
61. 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
67. Data Manager
Implements a single Repository interface to handle all the data sources
required for a specific set of data
68. 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
69. 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
71. 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
72. 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
80. 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
81. 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
82. 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
83. Next thing you can do!
Consider the separation between data, domain, and
presentation logic in one of your projects