Created
March 28, 2017 16:20
-
-
Save firefoxrebo/233f5adab761f3ef1331b5bddf5667bd to your computer and use it in GitHub Desktop.
Descriping the factory method design pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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