SlideShare a Scribd company logo
1 of 48
Download to read offline
1
Adapting clean architecture
in Android apps
Matso Abgaryan
@AbgaryanMr
linkedin.com/in/matsoab
2
Part one: Clean Architecture
Part two: Clean Architecture in Android
3
What is architecture
4
● Testable
● Independent of the data sources
● Independent of libraries & frameworks
● Independent of the UI
Why should one use a Clean Architecture
5
“Clean Architecture separates the application code into layers and these layers define the
Separation of Concerns inside the codebase.”
Uncle Bob
What is clean architecture
6
Characteristics of a Clean Architecture
Entities
Use Cases
Controllers
G
ateways
P
r
e
s
e
n
t
e
r
s
DB
UI
Web
External
Interfaces
Devices
■ Enterprise Business Rules
■ Application Business Rules
■ Interface Adapters
■ Framework & Drivers
7
● Single Responsibility Principle
● Open-Closed Principle
● Liskov Substitution Principle
● Interface Segregation Principle
● Dependency Inversion Principle
What are SOLID design principles
8
■ A class should have only one reason to change.
What is Single-Responsibility principle
9
fun onBind(trip: Trip) {
itemView.start_name.text = trip.start
itemView.destination_name.text = trip.destination
val formattedDate = DateFormat.getDateInstance().format(
trip.departureTime,
StringBuffer("departure Time : "),
FieldPosition(0)
)
itemView.departure_time.text = formattedDate
}
Bad Example
10
fun onBind(trip: Trip) {
with(itemView) {
start_name.text = trip.start
destination_name.text = trip.destination
departure_time.text = dateUtil.formatDate(trip.departureTime)
}
}
Good Example
11
■ Software entities such as classes, functions, modules should be open
for extension but not modification.
What is Open-closed principle
12
class TimeGreeting(val time :String) {
fun greetingFromTimeOfDay(): String = when (time) {
"Morning" -> {
"Good Morning, sir."
}
"Afternoon" -> {
"Good Afternoon, sir."
}
"Evening" -> {
"Good Evening, sir."
}
else -> {
"Good Night, sir."
}
}
}
Bad Example
13
Good Example
interface Time {
fun greet(): String
}
class Morning : Time {
override fun greet(): String {
return "Good morning, sir."
}
}
class Afternoon : Time {
override fun greet(): String {
return "Good afternoon, sir."
}
}
class Evening : Time {
override fun greet(): String {
return "Good evening, sir."
}
}
class Night : Time {
override fun greet(): String {
return "Good night, sir."
}
}
class TimeGreeting(val time: Time) {
fun greetingFromTimeOfDay(): String =
time.greet()
}
14
■ Child classes should never break the parent class’ type definitions.
What is Liskov Substitution principle
15
open class Rectangle {
private var width: Int = 0
private var height: Int = 0
open fun setWidth(newWidth: Int) {
width = newWidth
}
open fun setHeight(newHeight: Int) {
height = newHeight
}
fun getArea(): Int = width * height
}
Bad Example
val rectangle : Rectangle = Square()
rectangle.setWidth(3)
rectangle.setHeight(5)
rectangle.getArea()
class Square : Rectangle() {
override fun setWidth(newWidth: Int) {
super.setWidth(newWidth)
super.setHeight(newWidth)
}
override fun setHeight(newHeight: Int) {
super.setWidth(newHeight)
super.setHeight(newHeight)
}
}
16
interface Shape {
fun getArea(): Int
}
class Square: Shape {
private var diameter: Int = 0
fun setDiameter(diameter: Int) {
this.diameter = diameter
}
override fun getArea(): Int {
return diameter * diameter
}
}
class Rectangle: Shape {
private var width: Int = 0
private var height: Int = 0
fun setWidth(width: Int) {
this.width = width
}
fun setHeight(height: Int) {
this.height = height
}
override fun getArea(): Int {
return width * height
}
}
Good Example
17
■ The interface-segregation principle (ISP) states that no client should be forced
to depend on methods it does not use.
What is Interface segregation principle
18
interface OnClickListener {
fun onClick()
fun onLongClick()
}
class Button: OnClickListener {
override fun onClick() {}
override fun onLongCLick() {
//does not support Long clicks
}
}
class LongClickButton: OnClickListener{
override fun onClick() {
// does not support short clicks
}
override fun onLongCLick() {}
}
Bad Example
19
interface OnClickListener {
fun onClick()
}
interface OnLongClickListener {
fun onLongCLick()
}
class Button: OnClickListener {
override fun onClick() {}
}
class LongClickButton: OnLongClickListener {
override fun onLongCLick() {}
}
Good Example
20
■ High-level modules should not depend on low-level modules. Both should depend on
abstractions. Abstractions should not depend upon details. Details should depend upon
abstractions.
What is Dependency Inversion principle
21
class WhatsUpMessage {
var whatsUpText:String = ""
}
class MessageBoard {
var message: WhatsUpMessage? = null
fun printMessage() {
println(message?.whatsUpText)
}
}
Bad Example
22
interface IMessage {
fun printMessage()
}
class WhatsUpMessage:IMessage{
override fun printMessage() {}
}
class FaceBookMessage:IMessage{
override fun printMessage() {}
}
class MessageBoard {
var message: IMessage? = null
fun printMessage(){
message?.printMessage()
}
}
Good Example
23
● RRP: The Reuse/Release Equivalence Principle
● CCP: The Common Closure Principle
● CRP: The Common Reuse Principle
What are RRP, CCP, and CRP component principles
24
❏ The granularity of reuse is the granularity of release
What is The Reuse/Release Equivalence Principle
25
❏ Gather into components those classes that change for the same reasons and at the same times
(related to SRP)
What is The Common Closure Principle
26
❏ Don't force users of a component to depend on things they don't need (related to ISP)
What is The Common Reuse Principle
27
The Tension Diagram For Component Cohesion
REP CCP
CRP
To many unneeded releases
To many
components change
Hard to reuse
28
Part one: Clean Architecture
Part two: Clean Architecture in Android
29
What are Clean Architecture layers in Android
■ Domain layer
■ Data layer
■ Presentation layer
Use Cases
Entities
Data Sources
ViewModels
Fragm
ents
A
c
t
i
v
i
t
i
e
s
Repository implementations
30
How to organize Modules in clean way
Package by feature
Package by layer
31
Domain layer
Use Cases
Entities
Interactors
Data Sources
ViewModels
Fragm
ents
A
c
t
i
v
i
t
i
e
s
Repository implementations
32
sealed class Result<out T : Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Failure(val exception: Exception) : Result<Nothing>()
}
Domain layer in Android
Entities
data class User( val userId: String,
val displayName: String)
33
abstract class UseCase<out Type, in Params> where Type : Any {
abstract suspend operator fun invoke(params: Params): Result<Type>
}
Domain layer in Android
Use Cases
class LoginUseCase(private val loginRepository
: LoginRepository) :
UseCase<User, LoginUseCase.Params>() {
override suspend operator fun invoke(params: Params): Result<User> =
loginRepository
.login(password = params
.password, userName = params
.userName)
data class Params(val userName: String, val password: String)
}
34
interface LoginRepository {
suspend fun login(userName: String,
password: String): Result<User>
}
Domain layer in Android
Repositories
35
data class LoginInteractors(
val loginUseCase: LoginUseCase,
val validateUserNameUseCase: ValidateUserNameUseCase,
val validatePasswordUseCase: ValidatePasswordUseCase
)
Domain layer in Android
Interactors
36
Data layer
Use Cases
Entities
Interactors
Data Sources
ViewModels
Fragm
ents
A
c
t
i
v
i
t
i
e
s
Repository implementations
M
a
p
p
e
r
s
37
interface UserMapper {
fun mapToUser(loggedInUser: LoggedInUser): User
}
Data layer in Android
Data layer in Android
Mappers
class UserMapperImpl() : UserMapper {
override fun mapToUser(loggedInUser: LoggedInUser): User {
return User(
userId = loggedInUser.userId,
displayName = loggedInUser.displayName
)
}
}
38
class LoginDataSource {
fun login(username: String, password: String): LoggedInUser {
return LoggedInUser(UUID.randomUUID(), "Jane Doe")
}
}
Data layer in Android
Data Sources
39
class LoginRepositoryImpl
(
private val dataSource: LoginDataSource
,
private val userMapper: UserMapper
) : LoginRepository {
override suspend fun login(userName: String, password: String): Result<User> {
return try {
val loggedInUser = dataSource.login(userName, password)
val user = userMapper.mapToUser(loggedInUser)
Result.Success(user)
} catch (e: Throwable) {
Result.Failure(IOException("Error logging in"
, e))
}
}
...
}
Data layer in Android
Repository implementations
40
Presentation layer
Use Cases
Entities
Interactors
Data Sources
ViewModels
Fragm
ents
A
c
t
i
v
i
t
i
e
s
Repository implementations
M
a
p
p
e
r
s
41
data class LoginViewState(
val loginFormState: LoginFormState? = null,
val loginResult: LoginResult? = null
)
Presentation layer in Android
ViewState Entities
42
class LoginViewModel(
private val loginInteractors: LoginInteractors
) : ViewModel() {
private val viewState = MutableStateFlow(LoginViewState())
fun login(username: String, password: String) {
viewModelScope.launch {
val result = loginInteractors.loginUseCase.invoke(
LoginUseCase.Params(username,password)
)
val loginResult = when (result) {
is Result.Success ->
LoginResult(success = LoggedInUserView(displayName = result.data.displayName))
else -> LoginResult(error = result.error)
}
viewState.value = viewState.value.copy(loginResult = loginResult)
}
}
}
Presentation layer in Android
ViewModel
43
class LoginFragment : Fragment() {
...
override fun onViewCreated(view: View, savedInstanceState
: Bundle?) {
super.onViewCreated(view, savedInstanceState
)
...
loginViewModel
.collectLatest(lifecycleScope,::collectViewState
)
}
private fun collectViewState
(viewState: LoginViewState
) {
with(viewState.loginResult){
error
?.let{showLoginFailed
(it)}
success?.let{updateUiWithUser
(it)}
}
}
}
Presentation layer in Android
Fragment
44
Data flow between layers
Data layer
LoginRepositoryImpl LoginDataSource
UserMapper
Presentation layer
LoginViewModel
LoginFragment
Domain layer
LoginInteractors LoginUseCase
Result
LoginRepository
Many ways to implement
Boilerplate code
Not suitable for small projects
Learning curve
Pros and Cons Clean Architecture
Pros and Cons Clean Architecture
Testable
Independent of the data sources
Independent of libraries & frameworks
Independent of the UI
Pros Cons
46
What is Clean Architecture
Layers of a Clean Architecture
What are SOLID design principles
What are RRP,CCP and CRP component principles
The tension Diagram for component cohesion
Clean Architecture layers in Android
How to organize modules in Android
Data flow between layers in Android
Summary
Summary
47
Any questions ?
48
● https://www.blog.cleancoder.com
● Clean Architecture book by Robert Cecil Martin
● Working Effectively with Legacy Code Book by Michael C. Feathers
References
Matso Abgaryan
@AbgaryanMr
linkedin.com/in/matsoab

More Related Content

Similar to Adapting clean architecture in android apps

Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Palak Sanghani
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued Hitesh-Java
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;svivek p s
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.pptDeepVala5
 
Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...
Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...
Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...WebStackAcademy
 
Finding bugs that matter with Findbugs
Finding bugs that matter with FindbugsFinding bugs that matter with Findbugs
Finding bugs that matter with FindbugsCarol McDonald
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and UtilitiesPramod Kumar
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedPawanMM
 
Presentacion clean code
Presentacion clean codePresentacion clean code
Presentacion clean codeIBM
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxRAJASEKHARV10
 
Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Martin Chapman
 
Iterative architecture
Iterative architectureIterative architecture
Iterative architectureJoshuaRizzo4
 

Similar to Adapting clean architecture in android apps (20)

Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Litwin linq
Litwin linqLitwin linq
Litwin linq
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...
Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...
Core Java Programming Language (JSE) : Chapter IX - Collections and Generic F...
 
JavaProgrammingManual
JavaProgrammingManualJavaProgrammingManual
JavaProgrammingManual
 
Finding bugs that matter with Findbugs
Finding bugs that matter with FindbugsFinding bugs that matter with Findbugs
Finding bugs that matter with Findbugs
 
JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
 
AngularJS in large applications - AE NV
AngularJS in large applications - AE NVAngularJS in large applications - AE NV
AngularJS in large applications - AE NV
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
 
Presentacion clean code
Presentacion clean codePresentacion clean code
Presentacion clean code
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Responsible JavaScript
Responsible JavaScriptResponsible JavaScript
Responsible JavaScript
 
Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Iterative architecture
Iterative architectureIterative architecture
Iterative architecture
 

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 

Recently uploaded (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 

Adapting clean architecture in android apps

  • 1. 1 Adapting clean architecture in Android apps Matso Abgaryan @AbgaryanMr linkedin.com/in/matsoab
  • 2. 2 Part one: Clean Architecture Part two: Clean Architecture in Android
  • 4. 4 ● Testable ● Independent of the data sources ● Independent of libraries & frameworks ● Independent of the UI Why should one use a Clean Architecture
  • 5. 5 “Clean Architecture separates the application code into layers and these layers define the Separation of Concerns inside the codebase.” Uncle Bob What is clean architecture
  • 6. 6 Characteristics of a Clean Architecture Entities Use Cases Controllers G ateways P r e s e n t e r s DB UI Web External Interfaces Devices ■ Enterprise Business Rules ■ Application Business Rules ■ Interface Adapters ■ Framework & Drivers
  • 7. 7 ● Single Responsibility Principle ● Open-Closed Principle ● Liskov Substitution Principle ● Interface Segregation Principle ● Dependency Inversion Principle What are SOLID design principles
  • 8. 8 ■ A class should have only one reason to change. What is Single-Responsibility principle
  • 9. 9 fun onBind(trip: Trip) { itemView.start_name.text = trip.start itemView.destination_name.text = trip.destination val formattedDate = DateFormat.getDateInstance().format( trip.departureTime, StringBuffer("departure Time : "), FieldPosition(0) ) itemView.departure_time.text = formattedDate } Bad Example
  • 10. 10 fun onBind(trip: Trip) { with(itemView) { start_name.text = trip.start destination_name.text = trip.destination departure_time.text = dateUtil.formatDate(trip.departureTime) } } Good Example
  • 11. 11 ■ Software entities such as classes, functions, modules should be open for extension but not modification. What is Open-closed principle
  • 12. 12 class TimeGreeting(val time :String) { fun greetingFromTimeOfDay(): String = when (time) { "Morning" -> { "Good Morning, sir." } "Afternoon" -> { "Good Afternoon, sir." } "Evening" -> { "Good Evening, sir." } else -> { "Good Night, sir." } } } Bad Example
  • 13. 13 Good Example interface Time { fun greet(): String } class Morning : Time { override fun greet(): String { return "Good morning, sir." } } class Afternoon : Time { override fun greet(): String { return "Good afternoon, sir." } } class Evening : Time { override fun greet(): String { return "Good evening, sir." } } class Night : Time { override fun greet(): String { return "Good night, sir." } } class TimeGreeting(val time: Time) { fun greetingFromTimeOfDay(): String = time.greet() }
  • 14. 14 ■ Child classes should never break the parent class’ type definitions. What is Liskov Substitution principle
  • 15. 15 open class Rectangle { private var width: Int = 0 private var height: Int = 0 open fun setWidth(newWidth: Int) { width = newWidth } open fun setHeight(newHeight: Int) { height = newHeight } fun getArea(): Int = width * height } Bad Example val rectangle : Rectangle = Square() rectangle.setWidth(3) rectangle.setHeight(5) rectangle.getArea() class Square : Rectangle() { override fun setWidth(newWidth: Int) { super.setWidth(newWidth) super.setHeight(newWidth) } override fun setHeight(newHeight: Int) { super.setWidth(newHeight) super.setHeight(newHeight) } }
  • 16. 16 interface Shape { fun getArea(): Int } class Square: Shape { private var diameter: Int = 0 fun setDiameter(diameter: Int) { this.diameter = diameter } override fun getArea(): Int { return diameter * diameter } } class Rectangle: Shape { private var width: Int = 0 private var height: Int = 0 fun setWidth(width: Int) { this.width = width } fun setHeight(height: Int) { this.height = height } override fun getArea(): Int { return width * height } } Good Example
  • 17. 17 ■ The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use. What is Interface segregation principle
  • 18. 18 interface OnClickListener { fun onClick() fun onLongClick() } class Button: OnClickListener { override fun onClick() {} override fun onLongCLick() { //does not support Long clicks } } class LongClickButton: OnClickListener{ override fun onClick() { // does not support short clicks } override fun onLongCLick() {} } Bad Example
  • 19. 19 interface OnClickListener { fun onClick() } interface OnLongClickListener { fun onLongCLick() } class Button: OnClickListener { override fun onClick() {} } class LongClickButton: OnLongClickListener { override fun onLongCLick() {} } Good Example
  • 20. 20 ■ High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. What is Dependency Inversion principle
  • 21. 21 class WhatsUpMessage { var whatsUpText:String = "" } class MessageBoard { var message: WhatsUpMessage? = null fun printMessage() { println(message?.whatsUpText) } } Bad Example
  • 22. 22 interface IMessage { fun printMessage() } class WhatsUpMessage:IMessage{ override fun printMessage() {} } class FaceBookMessage:IMessage{ override fun printMessage() {} } class MessageBoard { var message: IMessage? = null fun printMessage(){ message?.printMessage() } } Good Example
  • 23. 23 ● RRP: The Reuse/Release Equivalence Principle ● CCP: The Common Closure Principle ● CRP: The Common Reuse Principle What are RRP, CCP, and CRP component principles
  • 24. 24 ❏ The granularity of reuse is the granularity of release What is The Reuse/Release Equivalence Principle
  • 25. 25 ❏ Gather into components those classes that change for the same reasons and at the same times (related to SRP) What is The Common Closure Principle
  • 26. 26 ❏ Don't force users of a component to depend on things they don't need (related to ISP) What is The Common Reuse Principle
  • 27. 27 The Tension Diagram For Component Cohesion REP CCP CRP To many unneeded releases To many components change Hard to reuse
  • 28. 28 Part one: Clean Architecture Part two: Clean Architecture in Android
  • 29. 29 What are Clean Architecture layers in Android ■ Domain layer ■ Data layer ■ Presentation layer Use Cases Entities Data Sources ViewModels Fragm ents A c t i v i t i e s Repository implementations
  • 30. 30 How to organize Modules in clean way Package by feature Package by layer
  • 31. 31 Domain layer Use Cases Entities Interactors Data Sources ViewModels Fragm ents A c t i v i t i e s Repository implementations
  • 32. 32 sealed class Result<out T : Any> { data class Success<out T : Any>(val data: T) : Result<T>() data class Failure(val exception: Exception) : Result<Nothing>() } Domain layer in Android Entities data class User( val userId: String, val displayName: String)
  • 33. 33 abstract class UseCase<out Type, in Params> where Type : Any { abstract suspend operator fun invoke(params: Params): Result<Type> } Domain layer in Android Use Cases class LoginUseCase(private val loginRepository : LoginRepository) : UseCase<User, LoginUseCase.Params>() { override suspend operator fun invoke(params: Params): Result<User> = loginRepository .login(password = params .password, userName = params .userName) data class Params(val userName: String, val password: String) }
  • 34. 34 interface LoginRepository { suspend fun login(userName: String, password: String): Result<User> } Domain layer in Android Repositories
  • 35. 35 data class LoginInteractors( val loginUseCase: LoginUseCase, val validateUserNameUseCase: ValidateUserNameUseCase, val validatePasswordUseCase: ValidatePasswordUseCase ) Domain layer in Android Interactors
  • 36. 36 Data layer Use Cases Entities Interactors Data Sources ViewModels Fragm ents A c t i v i t i e s Repository implementations M a p p e r s
  • 37. 37 interface UserMapper { fun mapToUser(loggedInUser: LoggedInUser): User } Data layer in Android Data layer in Android Mappers class UserMapperImpl() : UserMapper { override fun mapToUser(loggedInUser: LoggedInUser): User { return User( userId = loggedInUser.userId, displayName = loggedInUser.displayName ) } }
  • 38. 38 class LoginDataSource { fun login(username: String, password: String): LoggedInUser { return LoggedInUser(UUID.randomUUID(), "Jane Doe") } } Data layer in Android Data Sources
  • 39. 39 class LoginRepositoryImpl ( private val dataSource: LoginDataSource , private val userMapper: UserMapper ) : LoginRepository { override suspend fun login(userName: String, password: String): Result<User> { return try { val loggedInUser = dataSource.login(userName, password) val user = userMapper.mapToUser(loggedInUser) Result.Success(user) } catch (e: Throwable) { Result.Failure(IOException("Error logging in" , e)) } } ... } Data layer in Android Repository implementations
  • 40. 40 Presentation layer Use Cases Entities Interactors Data Sources ViewModels Fragm ents A c t i v i t i e s Repository implementations M a p p e r s
  • 41. 41 data class LoginViewState( val loginFormState: LoginFormState? = null, val loginResult: LoginResult? = null ) Presentation layer in Android ViewState Entities
  • 42. 42 class LoginViewModel( private val loginInteractors: LoginInteractors ) : ViewModel() { private val viewState = MutableStateFlow(LoginViewState()) fun login(username: String, password: String) { viewModelScope.launch { val result = loginInteractors.loginUseCase.invoke( LoginUseCase.Params(username,password) ) val loginResult = when (result) { is Result.Success -> LoginResult(success = LoggedInUserView(displayName = result.data.displayName)) else -> LoginResult(error = result.error) } viewState.value = viewState.value.copy(loginResult = loginResult) } } } Presentation layer in Android ViewModel
  • 43. 43 class LoginFragment : Fragment() { ... override fun onViewCreated(view: View, savedInstanceState : Bundle?) { super.onViewCreated(view, savedInstanceState ) ... loginViewModel .collectLatest(lifecycleScope,::collectViewState ) } private fun collectViewState (viewState: LoginViewState ) { with(viewState.loginResult){ error ?.let{showLoginFailed (it)} success?.let{updateUiWithUser (it)} } } } Presentation layer in Android Fragment
  • 44. 44 Data flow between layers Data layer LoginRepositoryImpl LoginDataSource UserMapper Presentation layer LoginViewModel LoginFragment Domain layer LoginInteractors LoginUseCase Result LoginRepository
  • 45. Many ways to implement Boilerplate code Not suitable for small projects Learning curve Pros and Cons Clean Architecture Pros and Cons Clean Architecture Testable Independent of the data sources Independent of libraries & frameworks Independent of the UI Pros Cons
  • 46. 46 What is Clean Architecture Layers of a Clean Architecture What are SOLID design principles What are RRP,CCP and CRP component principles The tension Diagram for component cohesion Clean Architecture layers in Android How to organize modules in Android Data flow between layers in Android Summary Summary
  • 48. 48 ● https://www.blog.cleancoder.com ● Clean Architecture book by Robert Cecil Martin ● Working Effectively with Legacy Code Book by Michael C. Feathers References Matso Abgaryan @AbgaryanMr linkedin.com/in/matsoab