Blogger Information
Blog 42
fans 5
comment 0
visits 38595
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1205课程,单例类,工厂模式,依赖注入,面向接口编程
张浩刚
Original
837 people have browsed it

单例类

  1. <?php
  2. namespace _1205;
  3. use PDO;
  4. class demo{
  5. }
  6. $obj1 = new demo();
  7. $obj2 = new demo();
  8. var_dump($obj1 == $obj2); //true
  9. echo '<hr>';
  10. var_dump($obj1 === $obj2); //false
  11. echo '<hr>';
  12. //单例模式
  13. //当构造方法被私有化后,它本身就只能在内部实例化(new 类名),故它就是单例模式
  14. class Demo1{
  15. protected function __construct()
  16. {
  17. }
  18. protected static $instance;
  19. public static function getout(){
  20. is_null(static::$instance) ? static::$instance=new self() : '';
  21. //也可以用下面的方法
  22. // if(is_null(static::$instance)){
  23. // static::$instance=new self();
  24. // }
  25. return static::$instance;
  26. }
  27. }
  28. $obj1 = Demo1::getout();
  29. $obj2 = Demo1::getout();
  30. var_dump($obj1 === $obj2);
  31. echo '<br>';
  32. var_dump($obj1, $obj2);
  33. echo '<hr>';
  34. //单例模式应用场景 连接数据库
  35. class Db{
  36. private static $pdo = null;
  37. public static function getDate(...$connectParam){
  38. //这里类实例化new self(...$connectParam), 就是为了把它传参给构造方法
  39. //is_null是为了验证第一行的$pdo是否被实例化,没有就实例化并传参给构造方法
  40. is_null(static::$pdo) ? new self(...$connectParam) : '';
  41. return static::$pdo;
  42. }
  43. protected function __construct(...$connectParam){
  44. $dsn = $connectParam[0];
  45. $user = $connectParam[1];
  46. $password = $connectParam[2];
  47. static::$pdo = new PDO($dsn,$user,$password);
  48. }
  49. //禁止使用克隆魔术
  50. public function __clone()
  51. {
  52. }
  53. }
  54. $connectParam = ['mysql:host=localhost;dbname=film', 'root', 'root'];
  55. $pdo = Db::getDate(...$connectParam);
  56. print_r($pdo);
  57. //查询数据库内容,也可以用query()
  58. print_r($pdo->query('SELECT * FROM `movies`')->fetchAll(PDO::FETCH_ASSOC));

工厂模式

  1. <?php
  2. namespace _1205;
  3. class demo2{
  4. //...
  5. }
  6. // file1.php
  7. $obj = new Demo2();
  8. // file2.php
  9. $obj = new Demo2();
  10. // file3.php
  11. $obj = new Demo2();
  12. //这样调用每次都要实例化一次,很麻烦,故采用工厂模式比较方便
  13. //工厂模式 implode() 将数组转换成字符串
  14. class text1{
  15. public function __construct($arg1)
  16. {
  17. echo '对象1=' . $arg1;
  18. }
  19. }
  20. class text2{
  21. public function __construct($arg1, $arg2)
  22. {
  23. echo '对象2=' . implode(', ', [$arg1, $arg2]);
  24. }
  25. }
  26. class text3{
  27. public function __construct($arg1, $arg2, $arg3)
  28. {
  29. echo '对象3=' . implode(', ', [$arg1, $arg2, $arg3]);
  30. }
  31. }
  32. //使用工厂模式,专门创建类实例/对象
  33. class Factory{
  34. public static function create($classname, ...$argments){
  35. return new $classname(...$argments);
  36. }
  37. }
  38. // 类名::class 完整的类名,命名空间
  39. Factory::create(text1::class, 100);
  40. echo '<br>';
  41. Factory::create(text2::class, 100,200);
  42. echo '<br>';
  43. Factory::create(text3::class, 100,200,300);

依赖注入

  1. <?php
  2. // 依赖注入:解决对象
  3. // 工作类
  4. class Person{
  5. //要依赖的外部对象
  6. private $car = null;
  7. public function __construct()
  8. {
  9. //在构造方法中将外部对象全部实例化
  10. //注入点在构造方法中
  11. $this->car = new Car();
  12. }
  13. public function work(){
  14. return $this->car->drive();
  15. }
  16. }
  17. // 依赖的外部类
  18. class car{
  19. public function drive(){
  20. return '开车上班';
  21. }
  22. }
  23. // 客服端实例化
  24. $person = new Person();
  25. echo $person->work();
  26. echo '<hr>';
  27. /*******************************************/
  28. // 最傻瓜的依赖输入方式二
  29. class Person1{
  30. private static $car = null;
  31. public function __construct(Car1 $car1)
  32. {
  33. static::$car = $car1;
  34. }
  35. public function work(){
  36. return static::$car->drive();
  37. }
  38. }
  39. class Car1{
  40. public function drive(){
  41. return '开皮卡去上班';
  42. }
  43. }
  44. $car1 = new Car1();
  45. $person1 = new Person1($car1);
  46. echo $person1->work();

自动连接 autoload.php

  1. <?php
  2. spl_autoload_register(function ($className){
  3. $path = str_replace('\\', '/', $className);
  4. require dirname(__DIR__) . DIRECTORY_SEPARATOR . $path . '.php';
  5. });

工厂模式小案例

  1. <?php
  2. namespace demo;
  3. use demo\Vehicle\car;
  4. use demo\Vehicle\bus;
  5. use demo\Vehicle\ship;
  6. require __DIR__ . '/autoload.php';
  7. // 工厂类, 专用于创建类实例
  8. class Factroy{
  9. protected static $intance = null;
  10. public static function getDrive($variable){
  11. switch ($variable) {
  12. case 'car':
  13. static::$intance = new car();
  14. break;
  15. case 'bus':
  16. static::$intance = new bus();
  17. break;
  18. case 'ship':
  19. static::$intance = new ship();
  20. break;
  21. }
  22. // 返回当前具体的交通工具
  23. return static::$intance;
  24. }
  25. }
  26. class drive{
  27. // 交通工具
  28. protected static $vehicle;
  29. // 构造方法 调用工厂,传参
  30. public function __construct($variable)
  31. {
  32. static::$vehicle = Factroy::getDrive($variable);
  33. }
  34. // 调用外部一个依赖对象
  35. public function trave(){
  36. return static::$vehicle->drive() . ' 美好生活';
  37. }
  38. }
  39. //客户端输出
  40. echo (new drive('ship'))->trave() . '<br>';
  41. echo (new drive('car'))->trave() . '<br>';
  42. echo (new drive('bus'))->trave() . '<br>';
  43. //#外部类################
  44. //#外部类################
  45. //#外部类################
  46. <?php
  47. namespace demo\Vehicle;
  48. class bus{
  49. public function drive(){
  50. return '坐公交车';
  51. }
  52. }
  53. ===============================
  54. <?php
  55. namespace demo\Vehicle;
  56. class car{
  57. public function drive(){
  58. return '开汽车';
  59. }
  60. }
  61. ===============================
  62. <?php
  63. namespace demo\Vehicle;
  64. class ship{
  65. public function drive(){
  66. return '坐轮船';
  67. }
  68. }

接口类 代替 工厂模式

  1. <?php
  2. namespace demo;
  3. use demo\Vehicle\train;
  4. use demo\Vehicle\plane;
  5. use demo\Vehicle\iVehicle;
  6. require __DIR__ . '/autoload.php';
  7. class travee{
  8. // 交通工具
  9. private static $vehicle;
  10. // 构造方法
  11. public function __construct(iVehicle $vehicle)
  12. {
  13. static::$vehicle = $vehicle;
  14. }
  15. // 调用外部一个依赖对象
  16. public function go()
  17. {
  18. return static::$vehicle->drive() . ' : ======= 去旅行';
  19. }
  20. }
  21. //客户端调用
  22. echo (new travee(new plane()))->go(),'<br>';
  23. echo (new travee(new train()))->go();
  24. //#####################
  25. //#####################
  26. //#####################
  27. <?php
  28. namespace demo\Vehicle;
  29. //接口
  30. interface iVehicle{
  31. public function drive();
  32. }
  33. ===========================
  34. <?php
  35. namespace demo\Vehicle;
  36. class plane implements iVehicle{
  37. public function drive(){
  38. return '坐飞机';
  39. }
  40. }
  41. ===========================
  42. <?php
  43. namespace demo\Vehicle;
  44. class train implements iVehicle{
  45. public function drive(){
  46. return '坐火车';
  47. }
  48. }

总结

本节课相对简单,今天好好做了一次,基本掌握了

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