CodeIgniter 4 employs several design patterns to enhance code organization, reusability, and maintainability. Here my note of the (7+2) prominent patterns:
1. Singleton Pattern
Purpose: Ensures that a class has only one instance and provides a global point of access to it.
Usage: CodeIgniter's CI_Loader class is a classic example of a singleton. It's used to load other classes and components within the framework.
2. Dependency Injection
Purpose: Promotes loose coupling between classes by passing dependencies as parameters to constructors or methods.
Usage: CodeIgniter uses dependency injection to inject necessary services into controllers and other classes. For instance, a controller might be injected with a database connection or a session handler.
3. Front Controller Pattern
Purpose: Centralizes request handling and routing.
Usage: The index.php file in CodeIgniter acts as the front controller. It intercepts all incoming requests, routes them to the appropriate controller, and handles the response.
4. Model-View-Controller (MVC) Pattern
Purpose: Separates concerns into three interconnected components: Model (data), View (presentation), and Controller (logic).
Usage: CodeIgniter adheres to the MVC architecture. Controllers handle user requests, models interact with data, and views present the output.
5. Observer Pattern
Purpose: Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
Usage: While not explicitly implemented in CodeIgniter's core, the observer pattern can be used to implement features like caching or event handling.
6. Factory Pattern
Purpose: Provides a way to create objects without specifying their exact class.
Usage: CodeIgniter's CI_Loader class can be seen as a factory for creating instances of other classes based on their names.
7. Facade Pattern
Purpose: Provides a unified interface to a set of interfaces in a subsystem.
Usage: While not a direct implementation, CodeIgniter's CI_Loader class can be considered a facade, simplifying the process of loading and accessing various components.
Additional:
- Middleware: CodeIgniter's middleware functionality can be seen as an application of the Chain of Responsibility pattern.
- Routing: The routing system in CodeIgniter employs a combination of patterns, including the Strategy pattern for different routing methods and the Composite pattern for grouping routes.
Are you interested in diving deeper into CodeIgniter 4 patterns used?
1. Singleton Pattern
Purpose: Ensures that a class has only one instance and provides a global point of access to it.
Usage: CodeIgniter's CI_Loader class is a classic example of a singleton. It's used to load other classes and components within the framework.
2. Dependency Injection
Purpose: Promotes loose coupling between classes by passing dependencies as parameters to constructors or methods.
Usage: CodeIgniter uses dependency injection to inject necessary services into controllers and other classes. For instance, a controller might be injected with a database connection or a session handler.
3. Front Controller Pattern
Purpose: Centralizes request handling and routing.
Usage: The index.php file in CodeIgniter acts as the front controller. It intercepts all incoming requests, routes them to the appropriate controller, and handles the response.
4. Model-View-Controller (MVC) Pattern
Purpose: Separates concerns into three interconnected components: Model (data), View (presentation), and Controller (logic).
Usage: CodeIgniter adheres to the MVC architecture. Controllers handle user requests, models interact with data, and views present the output.
5. Observer Pattern
Purpose: Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
Usage: While not explicitly implemented in CodeIgniter's core, the observer pattern can be used to implement features like caching or event handling.
6. Factory Pattern
Purpose: Provides a way to create objects without specifying their exact class.
Usage: CodeIgniter's CI_Loader class can be seen as a factory for creating instances of other classes based on their names.
7. Facade Pattern
Purpose: Provides a unified interface to a set of interfaces in a subsystem.
Usage: While not a direct implementation, CodeIgniter's CI_Loader class can be considered a facade, simplifying the process of loading and accessing various components.
Additional:
- Middleware: CodeIgniter's middleware functionality can be seen as an application of the Chain of Responsibility pattern.
- Routing: The routing system in CodeIgniter employs a combination of patterns, including the Strategy pattern for different routing methods and the Composite pattern for grouping routes.
Are you interested in diving deeper into CodeIgniter 4 patterns used?
![[Image: Screenshot-2024-09-01-at-20-55-54.png]](http://i.postimg.cc/1fhKNHp3/Screenshot-2024-09-01-at-20-55-54.png)
![[Image: Screenshot-2024-09-03-at-07-19-52.png]](http://i.postimg.cc/yYbWKwry/Screenshot-2024-09-03-at-07-19-52.png)
![[Image: Screenshot-2024-09-03-at-07-20-54.png]](http://i.postimg.cc/rp6m05Td/Screenshot-2024-09-03-at-07-20-54.png)