Blogger Information
Blog 30
fans 0
comment 1
visits 24065
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0218单例模式与工厂模式
Original
740 people have browsed it

单例模式

单例模式,就是不允许过多的实例化一个类,例如在连接数据库的时候,数据库的连接有最大上限1000,NEW一次PDO就会多一个类实例,前面的连接如果没释放,就会增加一个,达到最大连接数以后,后面的就不无法再连接数据库了

  1. <?php
  2. $pdo = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
  3. $pdo1 = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
  4. $pdo2 = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
  5. var_dump($pdo);
  6. echo '<br>';
  7. var_dump($pdo1);
  8. echo '<br>';
  9. var_dump($pdo2);

图:3个PDO对象,ID增加到了3

单例例子

  1. <?php
  2. class Db
  3. {
  4. private static $pdo;
  5. //创建一个私有的构造方法,防止外部实例化
  6. private function __construct(...$linkparams)
  7. {
  8. list($dsn,$username,$pwd) = $linkparams;
  9. self::$pdo = new PDO($dsn,$username,$pwd);
  10. }
  11. public static function getPDO(...$linkparams)
  12. {
  13. //检查PDO这个变量有没有被赋值,如果被赋值了就直接返回
  14. if(is_null(self::$pdo)){
  15. new self(...$linkparams);
  16. };
  17. return self::$pdo;
  18. }
  19. //防止克隆当前对象
  20. private function __clone()
  21. {
  22. }
  23. }
  24. $linkparams = ['mysql:host=localhost;dbname=phpedu','root','root'];
  25. $pdo3 = DB::getPDO(...$linkparams);
  26. var_dump($pdo3);
  27. echo '<br>';
  28. $pdo4 = DB::getPDO(...$linkparams);
  29. var_dump($pdo4);
  30. echo '<br>';
  31. $pdo5 = DB::getPDO(...$linkparams);
  32. var_dump($pdo5);

图:不管创建几个,它都是同一个ID,不会增加

工厂模式

  1. <?php
  2. //工厂模式,就是把一些流程分为多个步骤/理解为多个工厂部门也可以,然后由一个工厂类来区别/通知某个类运作
  3. class Car
  4. {
  5. public function Engine()
  6. {
  7. return '汽车引擎';
  8. }
  9. public function Tyre()
  10. {
  11. return '汽车车轮';
  12. }
  13. public function Body()
  14. {
  15. return '汽车车身';
  16. }
  17. }
  18. class Bus
  19. {
  20. public function Engine()
  21. {
  22. return '巴士引擎';
  23. }
  24. public function Tyre()
  25. {
  26. return '巴士车轮';
  27. }
  28. public function Body()
  29. {
  30. return '巴士车身';
  31. }
  32. }
  33. // 工厂模式1
  34. class Factory
  35. {
  36. private static $model;
  37. public static function getModel(string $model)
  38. {
  39. switch ($model) {
  40. case 'car':
  41. self::$model = new Car();
  42. break;
  43. case 'bus':
  44. self::$model = new Bus();
  45. break;
  46. default:
  47. die('错误的车型');
  48. }
  49. return self::$model;
  50. }
  51. }
  52. // 工厂模式2
  53. class Factory2
  54. {
  55. private static $class;
  56. public static function getModel($class)
  57. {
  58. self::$class = new $class();
  59. return self::$class;
  60. }
  61. }
  62. // 工厂模式3
  63. class Factory3
  64. {
  65. private $model;
  66. private $department;
  67. public function __construct($class,$department)
  68. {
  69. $this->department = $department;
  70. $this->model = Factory2::getModel($class);
  71. }
  72. public function getModel()
  73. {
  74. switch($this->department){
  75. case 'engine':
  76. return $this->model->Engine();
  77. break;
  78. case 'body':
  79. return $this->model->Body();
  80. break;
  81. case 'tyre':
  82. return $this->model->Tyre();
  83. break;
  84. }
  85. }
  86. }
  87. $car = Factory::getModel('car');
  88. echo '模式1'. $car->Body().'<hr>';
  89. $car1 = Factory2::getModel('car');
  90. echo '模式2'. $car1->Body().'<hr>';
  91. $bus1 = Factory2::getModel('bus');
  92. echo '模式2'. $bus1->Engine().'<hr>';
  93. $car = new Factory3('car','engine');
  94. echo '模式3'.$car->getModel().'<hr>';
  95. $car = new Factory3('car','body');
  96. echo '模式3'.$car->getModel();


抽象工厂

  1. <?php
  2. //抽象工厂 区分更加细化,而且可以关闭其中的任意一个接口,而不会影响到整体
  3. interface Engine
  4. {
  5. public function Engine();
  6. }
  7. class CarEngine implements Engine
  8. {
  9. public function Engine()
  10. {
  11. return '汽车引擎';
  12. }
  13. }
  14. class BusEngine implements Engine
  15. {
  16. public function Engine()
  17. {
  18. return '巴士引擎';
  19. }
  20. }
  21. interface Tyre
  22. {
  23. public function Tyre();
  24. }
  25. class CarTyre implements Tyre
  26. {
  27. public function Tyre()
  28. {
  29. return '汽车轮胎';
  30. }
  31. }
  32. class BusTyre implements Tyre
  33. {
  34. public function Tyre()
  35. {
  36. return '巴士轮胎';
  37. }
  38. }
  39. interface Body
  40. {
  41. public function Body();
  42. }
  43. // class CarBody implements Body
  44. // {
  45. // public function Body()
  46. // {
  47. // return '汽车车身';
  48. // }
  49. // }
  50. class BusBody implements Body
  51. {
  52. public function Body()
  53. {
  54. return '巴士车身';
  55. }
  56. }
  57. class Factory
  58. {
  59. private static $model;
  60. public static function getModel($model,$part)
  61. {
  62. if ($model === 'car') {
  63. switch ($part) {
  64. case 'engine':
  65. self::$model = new CarEngine();
  66. break;
  67. case 'tyre':
  68. self::$model = new CarTyre();
  69. break;
  70. case 'body':
  71. self::$model = new CarBody();
  72. break;
  73. }
  74. } elseif ($model === 'bus') {
  75. switch ($part) {
  76. case 'engine':
  77. self::$model = new BusEngine();
  78. break;
  79. case 'tyre':
  80. self::$model = new BusTyre();
  81. break;
  82. case 'body':
  83. self::$model = new BusBody();
  84. break;
  85. }
  86. }
  87. return self::$model;
  88. }
  89. }
  90. class A
  91. {
  92. private $model;
  93. private $part;
  94. public function __construct($model,$part)
  95. {
  96. $this->part = $part;
  97. $this->model = Factory::getModel($model,$part);
  98. }
  99. public function getPart()
  100. {
  101. switch ($this->part) {
  102. case 'engine':
  103. return $this->model->Engine();
  104. break;
  105. case 'tyre':
  106. return $this->model->tyre();
  107. break;
  108. case 'body':
  109. return $this->model->body();
  110. break;
  111. }
  112. }
  113. }
  114. $car = new A('car','engine');
  115. echo $car->getPart().'<hr>';
  116. // $car = new A('car','body');
  117. echo $car->getPart().'<hr>';
  118. $car = new A('bus','engine');
  119. echo $car->getPart().'<hr>';
  120. $car = new A('bus','body');
  121. echo $car->getPart().'<hr>';


工厂模式优点就是把一个常用的类放一起,使用方便,缺点就是不容易更改代码,抽象工厂模式更细化,代码方便修改,隐藏其中一个或几个类,对整个代码没有多大影响。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:抽象方法也并非没有缺点, 用哪个方式来创建类实例, 还是要看具体场景的要求
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post