Php simple factory mode example php design mode Getting Started Tutorial

Source: Internet
Author: User
Php simple factory mode example php design mode Getting Started Tutorial

  1. /**

  2. * Example
  3. *
  4. * A farm needs to sell fruit to the market
  5. * There are three types of fruits, apples, and grapes on the farm.
  6. * Our ideas: 1. fruits have multiple attributes, each of which is different. However, they share a common place. | growth, planting, receiving, and 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 get a fruit class. we need to get an instance of a fruit from a farmer to know how to grow, plant, receive, and eat it.
  9. */

  10. /**

  11. * Virtual product interface
  12. * Define the implementation methods.
  13. */

  14. Interface fruit {

  15. /**

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

  19. /**

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

  23. /**

  24. * Gains
  25. */
  26. Public function harvest ();

  27. /**

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

  32. /**

  33. * Define specific product categories for Apple
  34. * First, we need to implement the methods defined by the inherited interfaces.
  35. * Then define Apple's unique attributes and methods
  36. */
  37. Class apple implements fruit {

  38. // Apple tree has an age

  39. Private $ treeAge;

  40. // Apple Color

  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. // The age of the apple tree

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

  58. // Set the apple tree age

  59. Public function setTreeAge ($ age ){
  60. $ This-> treeAge = $ age;
  61. Return trie;
  62. }

  63. }

  64. /**

  65. * Define specific products
  66. * First, we need to implement the methods defined by the inherited interfaces.
  67. * Then define the unique attributes and methods of grapes.
  68. */
  69. Class grape implements fruit {

  70. // Check whether grapes have seeds

  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. // Seed values

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

  88. // Set seedless

  89. Public function setSeedLess ($ seed ){
  90. $ This-> seedLess = $ seed;
  91. Return true;
  92. }
  93. }

  94. /**

  95. * The farmer class is used to obtain the instantiated fruit.
  96. *
  97. */
  98. Class farmer {

  99. // Define a static factory method

  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 for obtaining 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. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.