In programming, a design pattern is a problem analysed and solved with outstanding practise and experience. According to Grady Booch in Core J2EE Patterns
Design patterns are simply defined solutions to common problems. Design patterns are not created by someone who sits in a room and decides to make one; instead, design patterns are proven solutions that are implemented over and over again through different projects. This re-use of the solution itself becomes a pattern. Don’t be intimidated by the fancy names given to design patterns, such as façade, singleton, or observer; they are exactly that: fancy names given to repeatable solutions.
In the world of software, a pattern is a tangible manifestation of an organization’s tribal memory.
While programming, you might have reached a point in your project where you realised that there was no way to move forward. Daily. We deal with a lot of issues, problems, and deadlocks. But the good news is that a lot of these problems have been encountered and analysed by other programmers, and their definite solution has been proposed. These solutions, in a vast sense, are referred to as “design patterns.” Or you can say that design patterns are like using other people’s experience and wisdom to solve a particular programming problem.
Design patterns propose solutions in an object-oriented manner, and they are language- and framework-independent. This means that we can equally use these patterns in more than one object-oriented language. They always focus on solutions that are flexible and reusable.
Some important design patterns used in the Joomla API are:
Singleton Pattern, (In this pattern, only one object can be instantiated in the class.This is mostly used for maintaining a database connection throughout the application.
Factory Pattern. It provides the best way to create an object without exposing the logic behind it to the user. It is the best approach to follow when dealing with massive classes, and the newly created objects could be accessed through a common interface.
MVC Pattern. The Model-View-Controller (MVC) pattern is an abbreviation for the Model-View-Controller (MVC) pattern.This pattern tends to break an application into three layers and then process the user’s request accordingly. Model typically deals with databases, View with presentations, and Controller with request processing.
Active Record Pattern In an active record pattern, a model should reflect the schema of the database table, and then it should also be an exact replica of the view. For example, in the process of saving a record to a database, your form field names should match those of the table schema obtained by the model. Then the record could be saved and updated using an ORM approach.
What if we are not using design patterns?
It is normal for problems to arise while coding from time to time.How to stop creating a database connection again and again, how the data could be acquired, and how it could be presented This is just an example, and these are the points that a coder tackles on a daily basis. Now, if we do not use design patterns, we must recreate the logic in each project, depending on the size, complexity, and time duration of the project.Design patterns formalise these problems and solutions and make them available to the programming community.
MVC is a renowned pattern that separates the presentation, logic, and data in a software application where there is also a user interface.
In the model-view-controller approach, a typical web application is divided into three layers: the presentation layer, the business logic layer, and the data persistence layer. Combining these layers in a single file increases the complexity of the code and makes it difficult for the coder to make changes to these layers and to troubleshoot an issue. MVC provides a definite solution for how these layers could be easily separated by using the Model, View, and Controller approach.
How MVC works:
First, the user makes a request through the browser, and in the MVC system, it is passed to the controller for processing. On the basis of the request, the controller passes the request to either model or view. If it is related to viewing any database record, the controller will first make a request to the model. The model will fetch the data on the basis of the query from the model and pass it to the controller. The controller then requests to view an appropriate layout in order to display the information obtained from the model.View responds with it, and then the controller sends the information back to the browser for display to the user. That’s how a typical MVC approach works.
The controller actually deals with business logic processing. It responds to events invoked by the user and invokes changes to the model. Essentially, both the controller and the view are dependent on the model.Please take a look at the following example.
public function delete()
$id = $_REQUEST['id'];
$model = $this->getModel();
We are just taking the user’s input to delete a specific record on the basis of the provided ID. It is just getting the data and passing it to the model for processing.
In Joomla, the model basically corresponds to the database schema. It presents the logic on which the application operates. It gets instructions regarding the information from the controller and responds to an information request sent by the view.
public function delete($id)
$valid = $validate_id($id);
In the preceding example, the model will receive data from the controller and process it.The model’s work is to interact with the database, and the controller is not responsible for such an operation.
In MVC, a view is indeed the presentation or user interface layer of a software model. It manages how the information is displayed. In Joomla, a view corresponds to HTML-based presentations. As the example below indicates, you only have to focus here on HTML markup rather than how to fetch data from a database.
h1>?php echo $item->title;?>/h1> p>?php echo $item->content;?>/p> div class="page">
The goal of this article was to gain some interest in design patterns, with the end result being to help improve coding practises where needed. This is especially true for PHP developers; because most PHP developers code procedurally, my intentions are to provide them with an alternative method of development.
Next: Going through the Joomla core structure