App Architectures
&
Better Coding Guidelines
Syed Qamar Abbas
Software Engineer – Android
https://www.linkedin.com/in/qamar4p
Email: qamar.dev@gmail.com
Content
• How to code better?
• The most important design patterns
• Anti patterns in software development
• SOLID principles of object-oriented programming by Uncle Bob
• Dependency Injection
• Why should you care about app architecture?
• App architectures MVC, MVP, MVVM
• Android specific libraries for inspiration
• MVVM Android Example
• References
How to code better?
• Write descriptive class, function and variable names
• Consistent name scheme
• Logical grouping (based on screens/ app features/ architecture layers)
• Don’t repeat code
• Make comments, particularly in hard-to-understand areas
• Break down a big task into smaller chunks
• Prefer standard libraries
• Use design patterns
The Most Important Design Patterns
• Creational design patterns
• Singleton
• Abstract Factory
• Builder
• Structural design patterns
• Adapter
• Bridge
• Proxy
• Behavioral design patterns
• Command
• Observer
Anti Patterns
• An AntiPattern is a literary form that describes a commonly occurring
solution to a problem that generates decidedly negative
consequences.
• Software Development AntiPatterns
• Spaghetti Code
• Dead End
• Cut-and-Paste Programming
• Software Architecture AntiPatterns
• Software Project Management AntiPatterns
S.O.L.I.D— the five basic principles of object-oriented programming and design by Uncle Bob
Need of
accountant
in future.
Changes
required
Continued…
DI Example
Problem
Solution
Dependency Injection?
• In software engineering, dependency injection is a technique whereby
one object (or static method) supplies the dependencies of another
object. A dependency is an object that can be used (a service).
Example (bad)
Example (Good)
How DI works
Swinject https://github.com/Swinject/Swinject ★ 3,2453
★ 13,554
Dependency Inversion vs Dependency
Injection
• Dependency Injection is an Inversion of Control technique for
supplying objects ('dependencies') to a class by way of the Dependency
Injection Design Pattern. Typically passing dependencies via one of the
following:
• A constructor
• A public property or field
• A public setter
• The Dependency Inversion Principle (DIP) is a software
design guideline which boils down to two recommendations about de-
coupling a class from its concrete dependencies:
• '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.'
Yes, DI means Dependency Injection
Libraries like dagger2 generated compile time code
Why should you care about app architecture?
• Enables quicker changes in a layer, without impacting others
• Higher adaptability. New technical features
• It allows for different teams to divide work.
• You are able to scale the application up and out.
• It adds reliability and more independence
• Easy maintenance of the code base
• Great testability
• Easy to understand
Application architecture (Continue…)
1. MVC: Model View Controller
2. MVP: Model View Presenter
3. MVVM: Model View ViewModel
4. MVI: Model-View-Intent
5. VIPER: View Interactor Presenter Entity Routing
6. FLUX and REDUX: well, just Redux, although it should be called
ActionCreator-Action-Dispatcher-Middleware-Reducer-Store-
Middleware-View
Application architecture (MVC)
• Model
the data layer, responsible for managing the business logic and handling
network or database API.
• View
the UI layer — a visualisation of the data from the Model.
• Controller
the logic layer, gets notified of the user’s behavior and updates the Model as
needed.
Application architecture (MVC)
• Distribution —the View and
the Model in fact separated,
but the View and
the Controller are tightly
coupled.
• Testability— due to the bad
distribution you’ll probably
only test your Model.
Application architecture (MVP)
• Model
Same as MVC / No change
• View
The only change here is that the Activity/Fragment is now considered
part of the view.
• Presenter
This is essentially the controller from MVC except that it is not at all tied to the
View, just an interface. This addresses the testability concerns as well as the
modularity/flexibility concerns we had with MVC. In fact, MVP purists would
argue that the presenter should never have any references to any Android APIs
or code.
MVP
MVC vs MVP
• MVP
• View is more loosely coupled to the model. The
presenter is responsible for binding the model to
the view.
• Easier to unit test because interaction with the view
is through an interface
• Usually view to presenter map one to one.
• MVC
• Controller are based on behaviors and can be
shared across views
• Can be responsible for determining which view to
display
Application architecture (MVVM)
• Model
Same as MVC / No change
• View
The view binds to observable variables and actions exposed by the viewModel
in a flexible way.
• ViewModel
The ViewModel is responsible for wrapping the model and preparing
observable data needed by the view.
Application architecture (MVVM)
• MVVM treats the view controller/activities/fragments as the View
• There is no tight coupling between the View and the Model
• In addition, it does binding like the Supervising version of the MVP
• Bindings
• Bindings come out of box for the OS X development
• In iOS KVO based SwiftBond binding can be used or ReactiveCocoa, RxSwift
• In Android DataBinding is supported in Studio (new name ViewBinding)
Application architecture (MVVM)
Android specific libraries (latest)
• Jetpack is a suite of libraries, tools, and guidance to help developers
write high-quality apps easier
• Navigation, ViewModel, LiveData, Room etc
• Retrofit, OkHttp
• RXJava2, Kotlin Coroutine
• Dagger2, Koin for Kotlin
• Junit, Expresso, mockito
• Gson, Moshi, Glide, Picasso
MVVM Android Example
• Pre-requisites
• Android Studio and Gradle v3.4.0
• Android SDK 28
• Android Build Tools v28.0.0
• Android Support Repository
• Architecture components
• Dagger2
• Live data
• RX JAVA and ViewModel
• Android architecture component
• Testing
• Mockito
• JUnit
Thank you!
Q&A
References
• https://medium.com/@karthikcsridhar/s-o-l-i-d-principles-in-java-
1aaff453d7ea
• https://proandroiddev.com/mvc-mvp-mvvm-clean-viper-redux-mvi-
prnsaaspfruicc-building-abstractions-for-the-sake-of-building-
18459ab89386
• https://medium.com/ios-os-x-development/ios-architecture-
patterns-ecba4c38de52
• https://www.raywenderlich.com/636803-mvvm-and-databinding-
android-design-patterns

Mobile App Architectures & Coding guidelines

  • 1.
    App Architectures & Better CodingGuidelines Syed Qamar Abbas Software Engineer – Android https://www.linkedin.com/in/qamar4p Email: qamar.dev@gmail.com
  • 2.
    Content • How tocode better? • The most important design patterns • Anti patterns in software development • SOLID principles of object-oriented programming by Uncle Bob • Dependency Injection • Why should you care about app architecture? • App architectures MVC, MVP, MVVM • Android specific libraries for inspiration • MVVM Android Example • References
  • 3.
    How to codebetter? • Write descriptive class, function and variable names • Consistent name scheme • Logical grouping (based on screens/ app features/ architecture layers) • Don’t repeat code • Make comments, particularly in hard-to-understand areas • Break down a big task into smaller chunks • Prefer standard libraries • Use design patterns
  • 4.
    The Most ImportantDesign Patterns • Creational design patterns • Singleton • Abstract Factory • Builder • Structural design patterns • Adapter • Bridge • Proxy • Behavioral design patterns • Command • Observer
  • 5.
    Anti Patterns • AnAntiPattern is a literary form that describes a commonly occurring solution to a problem that generates decidedly negative consequences. • Software Development AntiPatterns • Spaghetti Code • Dead End • Cut-and-Paste Programming • Software Architecture AntiPatterns • Software Project Management AntiPatterns
  • 6.
    S.O.L.I.D— the five basicprinciples of object-oriented programming and design by Uncle Bob
  • 8.
  • 9.
  • 13.
  • 14.
    Dependency Injection? • Insoftware engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service). Example (bad)
  • 15.
    Example (Good) How DIworks Swinject https://github.com/Swinject/Swinject ★ 3,2453 ★ 13,554
  • 16.
    Dependency Inversion vsDependency Injection • Dependency Injection is an Inversion of Control technique for supplying objects ('dependencies') to a class by way of the Dependency Injection Design Pattern. Typically passing dependencies via one of the following: • A constructor • A public property or field • A public setter • The Dependency Inversion Principle (DIP) is a software design guideline which boils down to two recommendations about de- coupling a class from its concrete dependencies: • '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.'
  • 17.
    Yes, DI meansDependency Injection Libraries like dagger2 generated compile time code
  • 18.
    Why should youcare about app architecture? • Enables quicker changes in a layer, without impacting others • Higher adaptability. New technical features • It allows for different teams to divide work. • You are able to scale the application up and out. • It adds reliability and more independence • Easy maintenance of the code base • Great testability • Easy to understand
  • 19.
    Application architecture (Continue…) 1.MVC: Model View Controller 2. MVP: Model View Presenter 3. MVVM: Model View ViewModel 4. MVI: Model-View-Intent 5. VIPER: View Interactor Presenter Entity Routing 6. FLUX and REDUX: well, just Redux, although it should be called ActionCreator-Action-Dispatcher-Middleware-Reducer-Store- Middleware-View
  • 20.
    Application architecture (MVC) •Model the data layer, responsible for managing the business logic and handling network or database API. • View the UI layer — a visualisation of the data from the Model. • Controller the logic layer, gets notified of the user’s behavior and updates the Model as needed.
  • 21.
    Application architecture (MVC) •Distribution —the View and the Model in fact separated, but the View and the Controller are tightly coupled. • Testability— due to the bad distribution you’ll probably only test your Model.
  • 22.
    Application architecture (MVP) •Model Same as MVC / No change • View The only change here is that the Activity/Fragment is now considered part of the view. • Presenter This is essentially the controller from MVC except that it is not at all tied to the View, just an interface. This addresses the testability concerns as well as the modularity/flexibility concerns we had with MVC. In fact, MVP purists would argue that the presenter should never have any references to any Android APIs or code.
  • 23.
  • 24.
    MVC vs MVP •MVP • View is more loosely coupled to the model. The presenter is responsible for binding the model to the view. • Easier to unit test because interaction with the view is through an interface • Usually view to presenter map one to one. • MVC • Controller are based on behaviors and can be shared across views • Can be responsible for determining which view to display
  • 25.
    Application architecture (MVVM) •Model Same as MVC / No change • View The view binds to observable variables and actions exposed by the viewModel in a flexible way. • ViewModel The ViewModel is responsible for wrapping the model and preparing observable data needed by the view.
  • 26.
    Application architecture (MVVM) •MVVM treats the view controller/activities/fragments as the View • There is no tight coupling between the View and the Model • In addition, it does binding like the Supervising version of the MVP • Bindings • Bindings come out of box for the OS X development • In iOS KVO based SwiftBond binding can be used or ReactiveCocoa, RxSwift • In Android DataBinding is supported in Studio (new name ViewBinding)
  • 27.
  • 28.
    Android specific libraries(latest) • Jetpack is a suite of libraries, tools, and guidance to help developers write high-quality apps easier • Navigation, ViewModel, LiveData, Room etc • Retrofit, OkHttp • RXJava2, Kotlin Coroutine • Dagger2, Koin for Kotlin • Junit, Expresso, mockito • Gson, Moshi, Glide, Picasso
  • 29.
    MVVM Android Example •Pre-requisites • Android Studio and Gradle v3.4.0 • Android SDK 28 • Android Build Tools v28.0.0 • Android Support Repository • Architecture components • Dagger2 • Live data • RX JAVA and ViewModel • Android architecture component • Testing • Mockito • JUnit
  • 31.
  • 32.
    References • https://medium.com/@karthikcsridhar/s-o-l-i-d-principles-in-java- 1aaff453d7ea • https://proandroiddev.com/mvc-mvp-mvvm-clean-viper-redux-mvi- prnsaaspfruicc-building-abstractions-for-the-sake-of-building- 18459ab89386 •https://medium.com/ios-os-x-development/ios-architecture- patterns-ecba4c38de52 • https://www.raywenderlich.com/636803-mvvm-and-databinding- android-design-patterns