To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Part 4.3Programming patterns

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.

YASS
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:

YASS
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.

YASS
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.

YASS
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

Code previewClose
Feedback 👍
Comments are sent via email to me.