hellosagar / android-interview-questions

I'm contributing to help others!
MIT License
49 stars 15 forks source link

SOLID Principles #32

Open hellosagar opened 2 years ago

hellosagar commented 2 years ago

Explained the SOLID principles in my language

hellosagar commented 2 years ago

S: Single responsibility principle - Class should do only one thing no more or less.

hellosagar commented 2 years ago

O: Open for extension close for modification


hellosagar commented 2 years ago

LSP1: https://www.youtube.com/watch?v=ObHQHszbIcE&list=PLrhzvIcii6GMNEfebXDXuphcdlZE1-qHG&index=2

LSP2: https://www.youtube.com/watch?v=bVwZquRH1Vk&list=PLrhzvIcii6GMNEfebXDXuphcdlZE1-qHG&index=4

L: Liskov's substitution - It states that Child classes should never break the parent class’ type definitions. This follows the Robosutness principles (Which means you should be liberal in what you accept and conservative in what you send) in a way that the pre-conditions should be the same or weaker, and post-conditions should be the same or stronger and the same goes for invariants should be same or stronger.

Here, Pre-conditions: It's a sort of entry condition for the method, to be able to call that method whatever is stated in the pre-conditions it must be true.

Post-conditions: It's on the other end of the spectrum, it says that whatever post-condition must be true when a method has been called.

Invariants: Stay true at all times in the program

Soln: Prefer composition over inheritance, it is predictable when there is a lot of subtyping to avoid this problem


hellosagar commented 2 years ago

(ISP: https://www.youtube.com/watch?v=xahwVmf8itI) I: Interface segregation-

(It's the easiest to understand because it's easy it's complex in a way that THAT's ALL IT IS?) It states better to have many small interfaces rather than a few large interfaces. or if you restate it too remember easily too many is better than too few.

Explaining problem statement:

Stay away from this: Screenshot 2022-10-02 at 3 46 14 PM

Prefer this: Screenshot 2022-10-02 at 3 46 46 PM

After having different species (advantage of using small and concise interfaces): Screenshot 2022-10-02 at 3 52 44 PM

Soln: You say what the benefits of operating it into different interfaces, we are favoring composition over inheritance and decoupling over coupling.

and in a practical thingy, if we have a other species which can only sleep and eat and not move, and if we're using inheritance then we've to create a new class but in case of small roles for each task, its easy to pick exactly which the class wants

hellosagar commented 2 years ago

D: Dependency inversion

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend upon details. Details should depend upon abstractions.

advantages:

  1. we can easily swap a low-level module with a different one because a high-level module and low-level module both depend on abstraction.
  2. Easily able to test in isolation, as we can pass the class specific to testing through the constructor. Example sending the dispatcher through constructor in ViewModel in the android instead of hard-coding dispatcher
  3. Minimized risk to affect old functionality, since we don’t change it.

references: