Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SOLID - Architecture and Architectural Decision...

SOLID - Architecture and Architectural Decisions - Devfest Goa 2024

SOLID principles have a bad reputation for being complex among many developers, in this session we'll try to break this myth.
We'll also explore how we use various architectural patterns and decisions directly influence SOLID, without us realizing it.
This is an interactive session. We'll start with the book definition of each SOLID principle and then ask the audience a few questions regarding some architectural decisions.

Rivu Chakraborty

October 19, 2024
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. • India’s first GDE (Google Developer Expert) for Kotlin •

    More than 12 years in the Industry • Mobile Architect @ JioCinema • Previously ◦ Byju’s ◦ Paytm ◦ Gojek ◦ Meesho • Author (wrote multiple Kotlin books) • Speaker • Community Person • Entrepreneur (?) 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Who am I? 󰞦
  2. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does Single Responsibility Mean 🌐https://www.rivu.dev/

    youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social One class/function/object should focus on a single thing
  3. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does Single Responsibility Mean 🌐https://www.rivu.dev/

    youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Let’s say you have a viewmodel, and along with maintaining the state, the viewmodel is doing business logic. Is it Single Responsibility?
  4. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does Single Responsibility Mean 🌐https://www.rivu.dev/

    youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social There’s a Repository in our app, which is fetching data from the server and saving it in the local database while passing it onto the presentation layer (VM). Is it Single Responsibility?
  5. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social classes, modules, and functions should be open for extension but closed for modification.
  6. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social You have chat app. You only had text chat till now, you’re adding support for image sharing. So you add a flag (isImage) in your Chat class. Is it following OCP?
  7. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social Same example, you added an enum on MediaType instead of the boolean flag Is it following OCP?
  8. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social Same example, you added an enum on MediaType instead of the boolean flag. To add various data (like image url etc), you have to keep editing the Message class. Is it following OCP?
  9. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social In the same chat app, you’re now adding Emoji 😃 support in Text Messages You Edited the TextMessage Class to do this. Is it following OCP?
  10. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social Objects of a superclass shall be replaceable with objects of its subclasses without breaking the application
  11. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social Same Example. You have chat app. You only had text chat till now, you’re adding support for various other types of messages. Instead of creating enum/flags. You decided to extend the existing Message class. Is it following Liskov Substitution?
  12. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social No code should be forced to depend on methods it does not use. In other words, child classes should not be forced to implement/extend super class methods that it doesn’t require.
  13. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social Same Example. You have chat app. You only had text chat till now, you’re adding support for various other types of messages. You decided to better create an interface. Interface code in next slide.
  14. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social interface IMessage { fun getMessageText(): String fun getMessageType(): MessageType fun getMediaUrl(): Url fun getMediaPreviewBase64(): Base64 fun getAudioVideoCodec(): Codec } //Is it following Interface Segregation?
  15. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Dependency Inversion / Inversion of Control

    🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social It’s not only Dependency Injection
  16. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Dependency Inversion / Inversion of Control

    🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social • Dependency Injection • Constructor Injection • Service Locator
  17. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Dependency Injection 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    • Injecting a Dependency to a class / object at creation time, without any additional code in the class • It’s mostly done with Annotation processors in Java / JVM
  18. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Dependency Injection 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    • Injecting a Dependency to a class / object at creation time, without any additional code in the class • It’s mostly done with Annotation processors in Java / JVM • Example: Dagger / Hilt / Spring DI etc / Kotlin-Inject
  19. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Constructor Injection 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    • Done with passing parameters in the constructor • It’s also a type of Service Locator • Vanilla implementation of Dependency Inversion is often done with Constructor Injection
  20. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Service Locator 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social

    • Where a class has to request for a Dependency, but doesn’t directly create it • Example is Koin, where it uses Delegation to create dependencies
  21. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @rivu@androiddev.social Which one is Better? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @rivu@androiddev.social • Dependency Injection • Constructor Injection • Service Locator