Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. // Explaining the Factory Method Pattern
  4. abstract class Student
  5. {
  6. public $name;
  7. public $level;
  8.  
  9. // The factory method doesn't know which type of certificates to issue
  10. // instead subclasses can define that type
  11. abstract public function issueCertificate();
  12. }
  13.  
  14. abstract class Certificate
  15. {
  16. public $score;
  17. abstract public function calculateResults();
  18. }
  19.  
  20. class ElementaryStudent extends Student
  21. {
  22. public function issueCertificate()
  23. {
  24. return new ElementaryCertificate();
  25. }
  26. }
  27.  
  28. class PreparatoryStudent extends Student
  29. {
  30. public function issueCertificate()
  31. {
  32. return new PreparatoryCertificate();
  33. }
  34. }
  35.  
  36. class ElementaryCertificate extends Certificate
  37. {
  38. public function calculateResults()
  39. {
  40.  
  41. }
  42. }
  43.  
  44. class PreparatoryCertificate extends Certificate
  45. {
  46. public function calculateResults()
  47. {
  48.  
  49. }
  50. }
  51.  
  52. $ahmed = new ElementaryStudent();
  53. $ahmed->name = 'Ahmed Mohammed Ibrahim';
  54. $ahmed->age = 5;
  55. $ahmedCertificate = $ahmed->issueCertificate();
  56. $ahmedCertificate->score = 100;
  57. var_dump($ahmedCertificate);
  58.  
  59. $mohammed = new PreparatoryStudent();
  60. $mohammed->name = 'Mohammed Mohammed Ibrahim';
  61. $mohammed->age = 12;
  62. $mohammedCertificate = $mohammed->issueCertificate();
  63. $mohammedCertificate->score = 170;
  64. var_dump($mohammedCertificate);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement