Home  >  Article  >  Backend Development  >  PHP simple factory pattern example PHP design pattern introductory tutorial

PHP simple factory pattern example PHP design pattern introductory tutorial

WBOY
WBOYOriginal
2016-07-25 08:52:501173browse
  1. /**

  2. * An example
  3. *
  4. * A farm wants to sell fruits to the market
  5. * There are three kinds of fruits on the farm, apples and grapes
  6. * We imagine: 1. Fruits have many attributes, and each attribute is different, but they have Common place | Growing, planting, receiving, eating
  7. * 2. New fruits may be added in the future. We need to define an interface to standardize the methods they must implement
  8. * 3. We need to obtain the class of a certain fruit, You need to get examples of a certain fruit from the farmer to know how to grow, plant, harvest and eat
  9. */

  10. /**

  11. * Virtual product interface class
  12. * Define the methods that need to be implemented
  13. */

  14. interface fruit{

  15. /**

  16. *Growth
  17. */
  18. public function grow();

  19. /**

  20. *Planting
  21. */
  22. public function plant();

  23. /**

  24. * Harvest
  25. */
  26. public function harvest();

  27. /**

  28. * Eat
  29. */
  30. public function eat();
  31. }

  32. /**

  33. * Define the specific product category Apple
  34. * First, we need to implement the methods defined by the inherited interface
  35. * Then define Apple-specific attributes and methods
  36. */
  37. class apple implements fruit{

  38. //苹果树有年龄

  39. private $treeAge;

  40. //苹果有颜色

  41. private $color;

  42. public function grow(){

  43. echo "grape grow";
  44. }

  45. public function plant(){

  46. echo "grape plant";
  47. }

  48. public function harvest(){

  49. echo "grape harvest";
  50. }

  51. public function eat(){

  52. echo "grape eat";
  53. }

  54. //取苹果树的年龄

  55. public function getTreeAge(){
  56. return $this->treeAge;
  57. }

  58. //设置苹果树的年龄

  59. public function setTreeAge($age){
  60. $this->treeAge = $age;
  61. return trie;
  62. }

  63. }

  64. /**

  65. * Define the specific product class Grape
  66. * First, we need to implement the methods defined by the inherited interface
  67. * Then define the unique attributes and methods of Grape
  68. */
  69. class grape implements fruit{

  70. //葡萄是否有籽

  71. private $seedLess;

  72. public function grow(){

  73. echo "apple grow";
  74. }

  75. public function plant(){

  76. echo "apple plant";
  77. }

  78. public function harvest(){

  79. echo "apple harvest";
  80. }

  81. public function eat(){

  82. echo "apple eat";
  83. }

  84. //有无籽取值

  85. public function getSeedLess(){
  86. return $this->seedLess;
  87. }

  88. //设置有籽无籽

  89. public function setSeedLess($seed){
  90. $this->seedLess = $seed;
  91. return true;
  92. }
  93. }

  94. /**

  95. *Farmer class is used to obtain instantiated fruits
  96. *
  97. */
  98. class farmer{

  99. //定义个静态工厂方法

  100. public static function factory($fruitName){
  101. switch ($fruitName) {
  102. case 'apple':
  103. return new apple();
  104. break;
  105. case 'grape':
  106. return new grape();
  107. break;
  108. default:
  109. throw new badFruitException("Error no the fruit", 1);
  110. break;
  111. }
  112. }
  113. }

  114. class badFruitException extends Exception{

  115. public $msg;
  116. public $errType;
  117. public function __construct($msg = '' , $errType = 1){
  118. $this->msg = $msg;
  119. $this->errType = $errType;
  120. }
  121. }

  122. /**

  123. * Method to obtain fruit instantiation
  124. */
  125. try{
  126. $appleInstance = farmer::factory('apple');
  127. var_dump($appleInstance);
  128. }catch(badFruitException $err){
  129. echo $err->msg . "_______" . $err->errType;
  130. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn