In programming a design pattern is a problem analyzed and solved with an outstanding practice 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 a design pattern 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 just 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 analyzed that there is no way to move forward. Daily. we deal with a lot of issues, problems and deadlocks. But the good news is, a lot of these problems have been encountered and analyzed by other programmers and their definite solution has been proposed. These solutions, in a vast term are referred to as design patterns. Or you can say that design patterns are like, using other’s experience and wisdom to solve a particular programming problem.
Design patterns propose solutions in object oriented manner and they are language / framework independent. This means that we can equally use these patterns in more than one object oriented language. They always focus on those solutions which are flexible and reusable.
Some important design patterns used in Joomla API are.
Singleton Pattern, ( In this patter, only one object is allowed to instantiate in class. This is mostly used in maintaining a database connection throughout the application. )
Factory Pattern. ( It provides a best way to create an object, without exposing the logic behind them to the user. It is a best approach followed when dealing with massive classes and the newly created objects could be accessed through a common interface. )
MVC Pattern. ( MVC Pattern is a short form for Model-View-Controller Pattern. This pattern tends to break an application into three layers and then process the user request accordingly. Model normally deals with database, while view with presentation and controller with processing requests. )
Active Record Pattern. ( In 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 database, your form field names should match to that of the table schema got 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 a natural process to occur problems while coding, 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 to which a coder tackles on daily basis. Now, if we are not utilizing design patterns, we have to re create logic each and every time in project, depending on project size, complexity and time duration. Design patterns formalize these problems and solutions and make them available to programming community.
MVC is a most renowned pattern which separates the presentation, logic, and data in a software application, where there is also a user interface.
In Model view controller approach, a typical web application is divided into three layers, Presentation layer,Business logic layer and Data persistence layer. Combining these layers together in a single file increases complexity of code, make it difficult for the coder to make changes to these layers and to troubleshoot an issue. MVC provides a definite solution of how these layers could be separated easily by using 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 view any database record, the controller will first make a request to model. Model will fetch the data on the basis of query, from the model and pass it to controller. Controller than makes a request to view for an appropriate layout to display the information it got from the model. View responds it back and then controller sends the information back to the browser for displaying to user. Thats how a typical MVC approach works.
Controller actually deals with business logic processing. It responds to events invoked by a user and invoke changes to the model. Basically controller and view both depend on model. Please have a look at the following example.
public function delete()
$id = $_REQUEST['id'];
$model = $this->getModel();
We are just taking here the user input to delete a specific record on the basis of provided id. It is just getting the data and passing it to model for processing.
In Joomla, the model basically corresponds to database schema. It presents the logic on which application operates. It gets instructions regarding the information from controller and respond to information request, sent by the view.
public function delete($id)
$valid = $validate_id($id);
In the above example, model is going to receive data from controller and is processing it. Model’s work is to interact with database and controller is not responsible for such an operation.
A view is indeed a presentation or user interface layer of a software model, in MVC. It manages how the information is displayed. In Joomla, a view corresponds to HTML based presentations. As the example indicates below that you only have to focus here on html markup rather than how to fetch data from database.
<h1><?php echo $item->title; ?></h1>
<p><?php echo $item->content; ?></p>
The goal of this article was to gain some interest in design patterns with the end result to help improve coding practices where need. This is especially so for PHP developers, purely for the fact most PHP developers code procedural, my intentions are to give these PHP developers and alternative way of method of development.
Next –> Going through Joomla core structure