When computer software is developed, one of the first things that is thought of is how to structure the different program components. To that end, several design patterns were developed to make coding easier and make sure that the components are kept separate from each other and organised.
Two of these models, both discussed in this part of the course, are very common in the software development industry. They are:
- The MVC model
- The singleton pattern
MVC
The MVC model is very common today with several web frameworks using it as the basis of their design such as CodeIgniter, Symfony and much more.
MVC stands for Model-View-Controller. It is a pattern designed to separate the data flow from the interface and the data.
Model
Specifically, the model is designed to represent some data structure in a format such as an object, that makes it easy to retrieve data. It is possible for the model to implement some logic, but often the model is simply designed to hold data, rather than process it.
Normally the contents of the model are retreived from a database.
The following is the code for a model representing a school pupil, notice it has getter and setter methods for each property it contains.
class Pupil private $forename = "" private $surname = "" function _construct($f, $s) this->$forename = $f this->$surname = $s end function public function setForename($f) this->$forename = $f //Code could be added to update the database with new details here end function public function setSurname($s) this->$surname = $s //Code could be added to update the database with new details here end function public function getForename() return this->$forename end function public function getSurname() return this->$surname end function end class
View
As the name suggests, the view is used to visualise the data. This could be through a graphical interface for example.
The following code creates a simple view that informs the user of the program when the data is changed through a console print line:
class PupilView public function updateDisplay($f, $s) print("Forename: " & $f) print("Surname: " & $s) end function end class
Controller
The controller sits in the middle of the model and view and allows the data to flow between. It is, crucially to the design of MVC at least, the main method of interaction with the rest of the program.
The controller contains properties/variables that represent both the model and the view, allowing it to directly communicate with each of them.
class PupilController private $model = null private $view = null function _construct($model, $view) this->$model = $model this->$view = $view end function public function getPupilForename() return this->$model->getForename() end function public function getPupilSurname() return this->$model->getSurname() end function public function setPupilForename($f) this->$model->setForename($f) end function public function setPupilSurname($s) this->$model->setSurname($s) end function public function updateView() this->$view->updateDisplay(this->$model->getForename(), this->$model->getSurname()) end function end class
The program
Now for the main program. This program mimics the operation of retrieving a pupil from a database. Instead of doing this, it simply creates a new pupil called Harry Thomson. This program assumes that the three previous code samples are included in it.
function main($args) $model = getPupil1() $view = new PupilView() $controller = new PupilController($model, $view) $controller->updateView() $controller->setPupilSurname("Pierce") $controller->updateView() end function function getPupil1() return new Pupil("Harry", "Thomson") end function
This code is available on the ZPE Online repository and can be found and run here.
The singleton pattern
The singleton pattern is a very special pattern that allows only one instance of an object to exist.
By using a static reference to the only instance, any time that a 'new' instance is attempted to be created, it will refer to itself. This is better explained with code (the static variables feature of ZPE hasn't actually been released as of yet, so running this in ZPE will not work):
class Singleton private static $instance = null public static function getInstance() if (Singleton::$instance == null) Singleton::$instance = new Singleton() end if return Singleton::$instance; end function //The constructor sets the static variable to an instance of itself public static function Singleton() Singleton::$instance = this end function end class $x = Singleton.getInstance()
The singleton pattern keeps the design of the code very organised and also reduces memory use (using static variables has this effect) whilst also giving easy access to a nearly global variable (since the variable is statically defined, the instance can be accessed by accessing the static variable, which as mentioned, can be accessed anywhere due to being statically defined).