Skip to content

Sayeed-H/SOLID-Principle-Laravel

Repository files navigation

SOLID Principles in Laravel

  • What is SOLID Principles?

    • Single Responsibility Principle (SRP) Each class in your project should have a single responsibility. For example, the controller should only be responsible for receiving and handling HTTP requests, the model should be responsible for handling database interactions, and the view should be responsible for displaying the data. You can achieve SRP by keeping the code modular and loosely coupled. Each module should have a clear and distinct responsibility and should not be intertwined with other modules.
    • Open-Closed Principle (OCP) The Open-Closed Principle states that a class should be open for extension but closed for modification. In other words, you should be able to add new functionality to a class without modifying its existing code. You can achieve OCP by using inheritance and polymorphism. Instead of modifying the existing code, you can create new classes that inherit from the existing classes and add new functionality.
    • Liskov Substitution Principle (LSP) The Liskov Substitution Principle states that if a class is a subtype of another class, it should be able to be used in place of its parent class without any unexpected behavior. You can achieve LSP by ensuring that all child classes have the same interface as their parent class.
    • Interface Segregation Principle (ISP) The Interface Segregation Principle states that a client should not be forced to depend on methods it does not use. You can achieve ISP by creating smaller, more specific interfaces for each class. Each class should only implement the methods it needs.
    • Dependency Inversion Principle (DIP) The Dependency Inversion Principle states that high-level modules should not depend on low-level modules. Both should depend on abstractions. You can achieve DIP by using dependency injection. Instead of instantiating objects inside a class, you can pass them in as dependencies. This allows you to swap out the implementation of a class without changing the code that uses it.
  • How SOLID principles could be applied to this project:

    1. Single Responsibility Principle (SRP): Each class should have only one reason to change. In this project, the classes that handle HTTP requests should not be responsible for business logic. The controllers should only handle the incoming requests and delegate the logic to the appropriate service classes.

    2. Open/Closed Principle (OCP): The code should be open for extension but closed for modification. This means that you should be able to add new features or change the behavior of the system without modifying the existing code. In this project, the controller and service classes should be designed to be easily extendable, without needing to modify the existing code.

    3. Liskov Substitution Principle (LSP): This principle states that any subclass should be able to substitute the parent class without affecting the correctness of the program. In this project, the different models and services should conform to the same interfaces, so that they can be interchanged without breaking the application.

    4. Interface Segregation Principle (ISP): This principle states that the clients should not be forced to depend on interfaces they do not use. In this project, the controllers and services should define interfaces that are specific to their needs. This will allow them to only depend on the methods they need, and not on methods that are not relevant to them.

    5. Dependency Inversion Principle (DIP): This principle states that high-level modules should not depend on low-level modules, but both should depend on abstractions. In this project, the services should depend on abstractions (interfaces) rather than concrete implementations. This will make it easier to change the implementations of the services without affecting the rest of the system

Releases

No releases published

Packages

No packages published

Languages