SOLID

Singleton se inventó lara tener una única instancia de un objeto no lara que fuera accesible desde todos los sitios

1. Single Responsibility Principle (SRP)

  • Definition: A class should have one and only one reason to change, meaning that a class should have only one job.

  • Ex: A class that calculates area of a shape also manages authentication, connection to db and rendering. WRONG!

  • Problems of GOD class: dependencies, different logics, not decoupled

  • Solution: Use layers, proper naming, DI

2. Open/Closed Principle (OCP):

  • Definition: Software entities (classes, modules, functions) should be open for extension but closed for modification

  • How to accomplish

    • Avoid depending on specific implementations, making use of abstract classes or interfaces.

  • Example in PHP:

class LoginService
{
    public function login($user)
    {
        if ($user instanceof User) {
            $this->authenticateUser($user);
        } else if ($user instanceOf ThirdPartyUser) {
            $this->authenticateThirdPartyUser($user);
        }
    }
}

3. Liskov Substitution Principle (LSP):

  • Definition: Objects of a derived class should be able to replace objects of the base class without affecting the correctness of the program.

  • A subtype can either be a class extending another class, or a class implementing an interface.

  • If we have a Tesla is a subtype of a Car, then I should be able to drive either a Car or a Tesla the same way.

  • Classes extensions must use the same prototype (method signatures) as their parents. When implementing an Interface there is a contract in the input but should be a contract on the output to. (Also exceptions)

    • You can widen the types of method arguments in child classes and interface implementations, but not make them more narrow.

    • You can make the return types in child classes and interface implementations more narrow, but not widen them.

    • In subtypes, you should try to avoid throwing exceptions that are not thrown by the parent class or documented on the interface being implemented (where possible).

    • You should consider closing off your constructor method arguments from modification in subtypes by making final

  • Example in PHP:

https://madewithlove.com/blog/liskov-substitution-principle-explained/#:~:text=Applying the Liskov Substitution Principle,narrow%2C but not widen them.

4. Interface Segregation Principle (ISP)

  • Definition: A client should never be forced to implement an interface that it doesn’t use

  • Many client-specific interfaces are better than one general-purpose interface

  • Solution:

    • Use dependencies

    • Split the class into => interfaces + abstractions

    • Implement abstractions in a way that don’t knoe about other classses’ behavior

=>

=>

5. Dependency Inversion Principle (DIP)

  • Definition: high-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details, details houls depend on abstractions

  • It encourages the use of interfaces or abstract classes to decouple components.

PasswordReminder => hig level

MySQLConnection => low level

"Abstractions should not depend on details, details should depend on abstractions."

Last updated