PHP Simple Factory mode sample PHP design pattern Getting Started Tutorial

Source: Internet
Author: User
  1. /**

  2. * One case
  3. *
  4. * A farm that sells fruit to the market
  5. * There are three kinds of fruit apples and grapes on the farm.
  6. * We envision: 1, fruit has a variety of properties, each attribute is different, but they have a common place | Growing, planting, receiving, eating
  7. * 2, in the future it is possible to add new fruits, we need to define an interface to standardize the methods they must implement
  8. * 3, we need to get a class of fruit, to get a sample of a fruit from the farmer to know how to grow, grow, receive, 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 specific product classes Apple
  34. * First, we want to implement the method defined by the inherited interface
  35. * Then define the properties that are unique to Apple, and how
  36. */
  37. Class Apple implements fruit{

  38. The apple tree has an age

  39. Private $treeAge;

  40. Apples have 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. Take the age of the apple tree

  55. Public Function Gettreeage () {
  56. return $this->treeage;
  57. }

  58. Set the age of the apple tree

  59. Public Function Settreeage ($age) {
  60. $this->treeage = $age;
  61. return trie;
  62. }

  63. }

  64. /**

  65. * Definition of specific product category grape
  66. * First, we want to implement the method defined by the inherited interface
  67. * Then define the properties specific to the grape, and the method
  68. */
  69. Class Grape implements fruit{

  70. Whether the 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. There is no seed to take value

  85. Public Function getseedless () {
  86. return $this->seedless;
  87. }

  88. Set with seeds and no seeds

  89. Public Function setseedless ($seed) {
  90. $this->seedless = $seed;
  91. return true;
  92. }
  93. }

  94. /**

  95. * farmers ' classes are used to obtain instanced 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. * Ways to get 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. }
Copy Code
  • Related Article

    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.