Slide 1

Slide 1 text

SOLID - Architecture and Architectural Decisions Rivu Chakraborty 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] https://topmate.io/rivu

Slide 2

Slide 2 text

● 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? 󰞦

Slide 3

Slide 3 text

SOLID

Slide 4

Slide 4 text

SOLID Single Responsibility

Slide 5

Slide 5 text

🌐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] One class/function/object should focus on a single thing

Slide 6

Slide 6 text

🌐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?

Slide 7

Slide 7 text

🌐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?

Slide 8

Slide 8 text

SOLID Open-closed principle

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

🌐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?

Slide 11

Slide 11 text

🌐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?

Slide 12

Slide 12 text

🌐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?

Slide 13

Slide 13 text

🌐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?

Slide 14

Slide 14 text

SOLID Liskov Substitution Principle

Slide 15

Slide 15 text

🌐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

Slide 16

Slide 16 text

🌐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?

Slide 17

Slide 17 text

SOLID Interface Segregation

Slide 18

Slide 18 text

🌐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.

Slide 19

Slide 19 text

🌐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.

Slide 20

Slide 20 text

🌐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?

Slide 21

Slide 21 text

SOLID Dependency Inversion

Slide 22

Slide 22 text

🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What Does it Mean? 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] A class that uses a dependency, should not create it

Slide 23

Slide 23 text

🌐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

Slide 24

Slide 24 text

🌐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

Slide 25

Slide 25 text

🌐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

Slide 26

Slide 26 text

🌐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

Slide 27

Slide 27 text

🌐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

Slide 28

Slide 28 text

🌐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

Slide 29

Slide 29 text

🌐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

Slide 30

Slide 30 text

🌐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

Slide 31

Slide 31 text

🌐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

Slide 32

Slide 32 text

🌐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

Slide 33

Slide 33 text

🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Which one is Better? 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] ● Dependency Injection ● Constructor Injection ● Service Locator

Slide 34

Slide 34 text

Thank You Rivu Chakraborty 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] https://topmate.io/rivu