Skip to content

Instantly share code, notes, and snippets.

@firefoxrebo
Created March 28, 2017 16:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firefoxrebo/233f5adab761f3ef1331b5bddf5667bd to your computer and use it in GitHub Desktop.
Save firefoxrebo/233f5adab761f3ef1331b5bddf5667bd to your computer and use it in GitHub Desktop.
Descriping the factory method design pattern
<?php
// Explaining the Factory Method Pattern
abstract class Student
{
public $name;
public $level;
// The factory method doesn't know which type of certificates to issue
// instead subclasses can define that type
abstract public function issueCertificate();
}
abstract class Certificate
{
public $score;
abstract public function calculateResults();
}
class ElementaryStudent extends Student
{
public function issueCertificate()
{
return new ElementaryCertificate();
}
}
class PreparatoryStudent extends Student
{
public function issueCertificate()
{
return new PreparatoryCertificate();
}
}
class ElementaryCertificate extends Certificate
{
public function calculateResults()
{
}
}
class PreparatoryCertificate extends Certificate
{
public function calculateResults()
{
}
}
$ahmed = new ElementaryStudent();
$ahmed->name = 'Ahmed Mohammed Ibrahim';
$ahmed->age = 5;
$ahmedCertificate = $ahmed->issueCertificate();
$ahmedCertificate->score = 100;
var_dump($ahmedCertificate);
$mohammed = new PreparatoryStudent();
$mohammed->name = 'Mohammed Mohammed Ibrahim';
$mohammed->age = 12;
$mohammedCertificate = $mohammed->issueCertificate();
$mohammedCertificate->score = 170;
var_dump($mohammedCertificate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment