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

Solid Principles

Solid Principles

Abid Jamil

May 11, 2022
Tweet

More Decks by Abid Jamil

Other Decks in Programming

Transcript

  1. Why Solid? It will make your project development more productiv

    e through decomposition and decoupling b y removing code smells an d reducing time spent on refactoring .
  2. Each principle addresses a speci fi c proble m and

    if your code does not have that problem there's no reason to apply the principle.
  3. SOLID principles are not something you can measure. There isn't

    something like saying   “My code is 100% SOLID"
  4. The idea behind the SRP is that every class, module,

    or function in a program should have one responsibility/purpose in a program.
  5. public class Student { public void registerStudent() { // some

    logic } public void calculate_Student_Results() { // some logic } public void sendEmail() { // some logic } }
  6. The code above will work perfectly but will lead to

    some challenges. We cannot make this code reusable for other classes or objects. The class has a whole lot of logic interconnected that we would have a hard time fi xing errors. And as the codebase grows, so does the logic, making it even harder to understand what is going on.
  7. public class StudentRegister { public void registerStudent() { // some

    logic } } public class StudentResult { public void calculate_Student_Result() { // some logic } }
  8. Now that we have separated the logic, our code is

    easier to understand as each core functionality has its own class. We can test for errors more ef fi ciently . The code is now reusable. Before, we could only use these functionalities inside one class but now they can be used in any class .
  9. The open-closed principle states that software entities should be open

    for extension, but closed for modi fi cation. This implies that such entities – classes, functions, and so on – should be created in a way that their core functionalities can be extended to other entities without altering the initial entity's source code .
  10. public class Human { public int height; public int weight;

    } public class CalculateBMI { public int CALCULATE_JOHN_BMI(Human John) { return John.height/John.weight; } }
  11. public class CalculateBMI { public int CALCULATE_HAIDER_BMI(Human Haider) { return

    Haider.height/Haider.weight; } public int CALCULATE_SALEEM_BMI(Human Saleem) { return Saleem.height/Saleem.weight; } }
  12. Although the code above may work perfectly, it's not ef

    fi cient. We modify the code constantly which may lead to bugs. And the code only has provision for humans. What if we have to calculate for an animal or an object?
  13. public interface Entity { public int CalculateBMI(); } public class

    Haider implements Entity { int height; int weight; public double CalculateBMI() { return Haider.height/Haider.weight; } } public class Saleem implements Entity { int height; int weight; public double CalculateBMI() { return Saleem.height/Saleem.weight; } }
  14. Now we no longer have to modify existing code when

    we create a new entity we just extend the functionality we need and apply it to the new entity.
  15. Let Φ(x) be a property provable about objects x of

    type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T. (Source: Wikipedia)
  16. The Liskov substitution principle simply implies that when an instance

    of a class is passed/ extended to another class, the inheriting class should have a use case for all the properties and behavior of the inherited class.
  17. public class Amphibian { public void swim(); public void walk();

    } public class Frog extends Amphibian { public void swim() { System.out.println("The frog is swimming"); } public void walk() { System.out.println("The frog is walking on land"); } }
  18. public class Amphibian { public void swim(); public void walk();

    } public class Shark extends Amphibian { public void swim() { System.out.println("The shark is swimming"); } public void walk() { System.out.println("The shark is walking on land"); } }
  19. The interface segregation principle states that the interface of a

    program should be split in a way that the user/client would only have access to the necessary methods related to their needs.
  20. public class Jane implements Teacher { @Override public void English()

    { System.out.println("Jane is teaching the students English language."); } @Override public void Biology() { } @Override public void Chemistry() { } @Override public void Mathematics() { } }
  21. public interface EnglishTeacher extends Teacher { void English(); } public

    interface BiologyTeacher extends Teacher { void Bilogy(); }
  22. public class Jane implements EnglishTeacher { @Override public void Teach()

    { System.out.println("Jane has started teaching."); } @Override public void English() { System.out.println("Jane is teaching the students English language."); } }
  23. public class Bank { public void GIVE_CUSTOMER_MONEY_OTC() { // some

    logic } } public class Customer { private Bank myBank = new Bank(); public void withdraw() { myBank.GIVE_CUSTOMER_MONEY_OTC(); } }
  24. public interface ATM { void ATM_OPERATION(); } public class Bank

    implements ATM { @Override ATM_OPERATION(){ // code to add money to ATM and increase the ATM balance } } public class Customer implements ATM { @Override ATM_OPERATION(){ // code to withdraw money from ATM and decrease the ATM balance } }