85
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

A Separation of Concerns: Clean Architecture on Android

Embed Size (px)

Citation preview

Page 1: A Separation of Concerns: Clean Architecture on Android

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

Page 2: A Separation of Concerns: Clean Architecture on Android

Why are we here?

To share with you our journey in applying Clean Architecture to an

Android project

...and get some valuable feedback

Page 3: A Separation of Concerns: Clean Architecture on Android

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

Page 4: A Separation of Concerns: Clean Architecture on Android
Page 5: A Separation of Concerns: Clean Architecture on Android

What are our goals as developers?

Easy to refactor on volatile projects

Maintainable

Page 6: A Separation of Concerns: Clean Architecture on Android

What are our goals as developers?

Easy to refactor on volatile projects

Maintainable

Be confident that the code is reliable

Testable

Page 7: A Separation of Concerns: Clean Architecture on Android

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

Page 8: A Separation of Concerns: Clean Architecture on Android

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

Page 9: A Separation of Concerns: Clean Architecture on Android

Introduction to Clean

Page 10: A Separation of Concerns: Clean Architecture on Android

What is Clean Architecture?

Page 12: A Separation of Concerns: Clean Architecture on Android

The Dependency Rule

Source code dependencies can only point inwards

Page 13: A Separation of Concerns: Clean Architecture on Android

How many layers?

(The answer is three)

Page 14: A Separation of Concerns: Clean Architecture on Android

Separation of Concerns

Presentation LogicDefines how to display visual information to the user

Examples:

● Organize screen layout

● Format text

● Enable user input

Page 15: A Separation of Concerns: Clean Architecture on Android

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

Page 16: A Separation of Concerns: Clean Architecture on Android

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

Page 17: A Separation of Concerns: Clean Architecture on Android

Architectural Diagram

Page 18: A Separation of Concerns: Clean Architecture on Android

Image Source: Lucasfilm Ltd. All Rights Reserved

Architectural Diagram

Page 19: A Separation of Concerns: Clean Architecture on Android

Domain Layer

Page 20: A Separation of Concerns: Clean Architecture on Android

Domain Layer

Page 21: A Separation of Concerns: Clean Architecture on Android

Entity

Represents a business object that concerns the applicationLeast likely to change when shifting requirements

Page 22: A Separation of Concerns: Clean Architecture on Android

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

Page 23: A Separation of Concerns: Clean Architecture on Android

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

Page 24: A Separation of Concerns: Clean Architecture on Android

Entity

Page 25: A Separation of Concerns: Clean Architecture on Android

Entity

Properties

Page 26: A Separation of Concerns: Clean Architecture on Android

Entity

Entity-specific logic

Page 27: A Separation of Concerns: Clean Architecture on Android

Use Case

Contains the business logic related to a specific use caseCan be run and canceled

Page 28: A Separation of Concerns: Clean Architecture on Android

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

Page 29: A Separation of Concerns: Clean Architecture on Android

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

Page 30: A Separation of Concerns: Clean Architecture on Android

Use Case

Page 31: A Separation of Concerns: Clean Architecture on Android

Use Case

One or more Repository objects

Page 32: A Separation of Concerns: Clean Architecture on Android

Use Case

May be run and cancelled

Page 33: A Separation of Concerns: Clean Architecture on Android

Use Case

Contains business logic

Page 34: A Separation of Concerns: Clean Architecture on Android

Use Case

Defines interactions

Page 35: A Separation of Concerns: Clean Architecture on Android

Repository

Asynchronous interface to abstract data access to a specific set of data from the data layer

Page 36: A Separation of Concerns: Clean Architecture on Android

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

Page 37: A Separation of Concerns: Clean Architecture on Android

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

Page 38: A Separation of Concerns: Clean Architecture on Android

Repository

Page 39: A Separation of Concerns: Clean Architecture on Android

Repository

Asynchronous

Page 40: A Separation of Concerns: Clean Architecture on Android

Repository

Callback definition

Page 41: A Separation of Concerns: Clean Architecture on Android

A Brief Note on Test-Driven-Development

Page 42: A Separation of Concerns: Clean Architecture on Android

Presentation Layer

Page 43: A Separation of Concerns: Clean Architecture on Android

Presentation Layer

Page 44: A Separation of Concerns: Clean Architecture on Android

Model-View-Presenter

Presenter

View Surface Activity

Presentation

Domain

Fragment

View

Implements

Uses

Uses

Uses

Page 45: A Separation of Concerns: Clean Architecture on Android

Presenter

Implements presentation logic by directing UI changes and handling user input

Page 46: A Separation of Concerns: Clean Architecture on Android

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

Page 47: A Separation of Concerns: Clean Architecture on Android

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

Page 48: A Separation of Concerns: Clean Architecture on Android

Presenter

Page 49: A Separation of Concerns: Clean Architecture on Android

Presenter

Systems Surfaces and Use Cases

Page 50: A Separation of Concerns: Clean Architecture on Android

Presenter

Lifecycle events

Page 51: A Separation of Concerns: Clean Architecture on Android

Presenter

UI event management

Page 52: A Separation of Concerns: Clean Architecture on Android

View Surface

Abstracts away the implementation of the UI logic, and is typically implemented by a View, Activity or Fragment

Page 53: A Separation of Concerns: Clean Architecture on Android

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

Page 54: A Separation of Concerns: Clean Architecture on Android

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

Page 55: A Separation of Concerns: Clean Architecture on Android

View Surface

Page 56: A Separation of Concerns: Clean Architecture on Android

View Surface

Navigation

Page 57: A Separation of Concerns: Clean Architecture on Android

View Surface

UI information

Page 58: A Separation of Concerns: Clean Architecture on Android

View Surface

UI state

Page 59: A Separation of Concerns: Clean Architecture on Android

System Surface

Abstracts away the implementation of various system functionalities

Page 60: A Separation of Concerns: Clean Architecture on Android

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

Page 61: A Separation of Concerns: Clean Architecture on Android

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

Page 62: A Separation of Concerns: Clean Architecture on Android

System Surface

Android System Dependencies

Abstracted System Specifics

Page 63: A Separation of Concerns: Clean Architecture on Android

System Surface

Android system dependencies

Page 64: A Separation of Concerns: Clean Architecture on Android

System Surface

System-specific implementation

Page 65: A Separation of Concerns: Clean Architecture on Android

Data Layer

Page 66: A Separation of Concerns: Clean Architecture on Android

Data Layer

Page 67: A Separation of Concerns: Clean Architecture on Android

Data Manager

Implements a single Repository interface to handle all the data sources required for a specific set of data

Page 68: A Separation of Concerns: Clean Architecture on Android

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

Page 69: A Separation of Concerns: Clean Architecture on Android

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

Page 70: A Separation of Concerns: Clean Architecture on Android

Client

Encapsulates a particular source of data and exposes an abstracted interface to the Data Managers

Page 71: A Separation of Concerns: Clean Architecture on Android

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

Page 72: A Separation of Concerns: Clean Architecture on Android

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

Page 73: A Separation of Concerns: Clean Architecture on Android

Mapper

Converts a data object (API or DB response) to a domain Entity

Page 74: A Separation of Concerns: Clean Architecture on Android

Responsibilities

Mapper

Converts a data object (API or DB response) to a domain Entity

● Transform data into domain-convenient Entities

Page 75: A Separation of Concerns: Clean Architecture on Android

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

Page 76: A Separation of Concerns: Clean Architecture on Android

Conclusions

Page 77: A Separation of Concerns: Clean Architecture on Android

Pros

Workflow improvements

Code readability

Testability

No more 1000+ line Activity classes

Isolated business logic

Possibility to work on separate layers in parallel

Page 78: A Separation of Concerns: Clean Architecture on Android

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

Page 79: A Separation of Concerns: Clean Architecture on Android

Further thoughts

Dagger 2! Can help a lot with keeping the layers clean

Dependency injection

Page 80: A Separation of Concerns: Clean Architecture on Android

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

Page 81: A Separation of Concerns: Clean Architecture on Android

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

Page 82: A Separation of Concerns: Clean Architecture on Android

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

Page 83: A Separation of Concerns: Clean Architecture on Android

Next thing you can do!

Consider the separation between data, domain, and presentation logic in one of your projects

Page 84: A Separation of Concerns: Clean Architecture on Android

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