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 @[email protected] Who am I? 󰞦
  2. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does Single Responsibility Mean 🌐https://www.rivu.dev/

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

    youtube.com/@rivutalks @rivuchakraborty @[email protected] 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?
  4. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @[email protected] 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?
  5. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

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

    @rivuchakraborty @[email protected] 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?
  7. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @[email protected] 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?
  8. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

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

    @rivuchakraborty @[email protected] 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?
  10. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @[email protected] 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.
  11. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

    @rivuchakraborty @[email protected] 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.
  12. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks

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

    🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] It’s not only Dependency Injection
  14. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Dependency Inversion / Inversion of Control

    🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] • Dependency Injection • Constructor Injection • Service Locator
  15. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Dependency Injection 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    • 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
  16. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Dependency Injection 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    • 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
  17. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Constructor Injection 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    • 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
  18. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Service Locator 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    • 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