php簡單原廠模式樣本 php設計模式入門教程

來源:互聯網
上載者:User
  1. /**

  2. * 一個案例
  3. *
  4. * 一個農場,要向市場銷售水果
  5. * 農場裡有三種水果 蘋果、葡萄
  6. * 我們設想:1、水果有多種屬性,每個屬性都有不同,但是,他們有共同的地方 | 生長、種植、收貨、吃
  7. * 2、將來有可能會增加新的水果、我們需要定義一個介面來規範他們必須實現的方法
  8. * 3、我們需要擷取某個水果的類,要從農場主那裡去擷取某個水果的執行個體,來知道如何生長、種植、收貨、吃
  9. */

  10. /**

  11. * 虛擬產品介面類
  12. * 定義好需要實現的方法
  13. */

  14. interface fruit{

  15. /**

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

  19. /**

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

  23. /**

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

  27. /**

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

  32. /**

  33. * 定義具體產品類 蘋果
  34. * 首先,我們要實現所繼承的介面所定義的方法
  35. * 然後定義蘋果所特有的屬性,以及方法
  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. * 定義具體產品類 葡萄
  66. * 首先,我們要實現所繼承的介面所定義的方法
  67. * 然後定義葡萄所特有的屬性,以及方法
  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. *農場主類 用來擷取執行個體化的水果
  96. *
  97. */
  98. class farmer{

  99. //定義個靜態Factory 方法

  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. * 擷取水果執行個體化的方法
  124. */
  125. try{
  126. $appleInstance = farmer::factory('apple');
  127. var_dump($appleInstance);
  128. }catch(badFruitException $err){
  129. echo $err->msg . "_______" . $err->errType;
  130. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.