Blogger Information
Blog 31
fans 0
comment 0
visits 18296
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP第19课-单列工厂模式和注入与面向接口-九期线上班
Content っ
Original
610 people have browsed it

1.将课堂代码全部上机操作至少一遍以上

demo1.php单例模式

  1. <?php
  2. namespace _1205;
  3. //单列模式
  4. class Demo1{
  5. //将构造函数设置为私有
  6. private function __construct()
  7. {
  8. }
  9. //定义单列成员
  10. private static $instance;
  11. //外部调用静态方法
  12. public static function init(){
  13. //判断是否为空
  14. if (is_null(static::$instance)){
  15. //为空创建
  16. static::$instance = new self();
  17. }
  18. //不为空返回
  19. return static::$instance;
  20. }
  21. //禁用克隆方法,防止外部通过克隆方法实例化
  22. private function __clone()
  23. {
  24. }
  25. }
  26. //实例化
  27. $d1 = Demo1::init();
  28. $d2 = Demo1::init();
  29. var_dump($d1,$d2);
  30. echo '<hr>';
  31. use PDO;
  32. //单列pdo类
  33. class Db{
  34. //单列pdo成员
  35. private static $pdo;
  36. //禁用构造函数
  37. private function __construct($connectParas=[])
  38. {
  39. static::$pdo = new PDO($connectParas[0],$connectParas[1],$connectParas[2]);
  40. }
  41. public static function init($connectParas=[]){
  42. if (is_null(static::$pdo)){
  43. new self($connectParas);
  44. }
  45. return static::$pdo;
  46. }
  47. //禁用克隆方法
  48. private function __clone()
  49. {
  50. }
  51. }
  52. $db1 = Db::init(['mysql:host=localhost;dbname=jason','root','root']);
  53. var_dump($db1);
  54. $result = $db1->query('select * from `account`',PDO::FETCH_ASSOC);
  55. foreach ($result as $row){
  56. echo '<pre>' . print_r($row, true) . '</pre>';
  57. }

demo2.php工厂模式

  1. <?php
  2. //工厂方法
  3. namespace _1205;
  4. use mvc\Facade;
  5. class Test1{
  6. public function __construct($a1){
  7. echo '对象创建成功,参数是'.$a1.'<hr>';
  8. }
  9. }
  10. class Test2{
  11. public function __construct($a1,$a2){
  12. echo '对象创建成功,参数是'.implode(',',[$a1,$a2]) .'<hr>';
  13. }
  14. }
  15. class Test3{
  16. public function __construct($a1,$a2,$a3){
  17. echo '对象创建成功,参数是'.implode(',',[$a1,$a2,$a3]).'<hr>';
  18. }
  19. }
  20. class Factory{
  21. public static function create($className,...$argments){
  22. return new $className(...$argments);
  23. }
  24. }
  25. Factory::create(Test1::class,100);
  26. Factory::create(Test2::class,100,200);
  27. Factory::create(Test3::class,100,200,300);

demo3.php注入复习

  1. <?php
  2. namespace _1205;
  3. //普通写法
  4. //人类
  5. class Person{
  6. //创建车成员
  7. public $car = null;
  8. public function __construct()
  9. {
  10. $this->car = new Car();
  11. }
  12. public function make(){
  13. $this->car->drive();
  14. }
  15. }
  16. //车类
  17. class Car{
  18. public function drive(){
  19. echo '开车去上班';
  20. }
  21. }
  22. //实例化
  23. $p1 = new Person();
  24. $p1->make();
  25. echo '<hr>';
  26. //依赖注入
  27. class Person1{
  28. //创建车成员
  29. public $car1 = null;
  30. public function __construct(Car1 $car1)
  31. {
  32. $this->car1 = $car1;
  33. }
  34. public function make(){
  35. $this->car1->drive();
  36. }
  37. }
  38. //车类
  39. class Car1{
  40. public function drive(){
  41. echo '开车去上班~~~~';
  42. }
  43. }
  44. //实例化
  45. $car1 = new Car1();
  46. $p2 = new Person1($car1);
  47. $p2->make();

Container.php容器

  1. <?php
  2. namespace _1205;
  3. class Container{
  4. public $instances = [];
  5. public function bind($alias,\Closure $closure){
  6. $this->instances[$alias] = $closure;
  7. }
  8. public function make($alias,$paras=[]){
  9. return $this->instances[$alias]();
  10. }
  11. }

Maker.php制作类

  1. <?php
  2. namespace _1205;
  3. class Maker{
  4. public function get(){
  5. return '苹果';
  6. }
  7. }

Product.php产品类

  1. <?php
  2. namespace _1205;
  3. // 商品类
  4. class Product
  5. {
  6. public function get(Maker $maker)
  7. {
  8. return '该手机是由: <span style="color:orangered;font-weight: bolder">' . $maker->get() . ' </span>生产的';
  9. }
  10. }

Client1.php项目类

  1. <?php
  2. namespace _1205;
  3. require 'Product.php';
  4. require 'Maker.php';
  5. class Client1{
  6. public function show(){
  7. //创建生产类
  8. $maker = new Maker();
  9. //创建
  10. $product = new Product();
  11. echo $product->get($maker);
  12. }
  13. }
  14. $c1 = new Client1();
  15. $c1->show();

Client2.php项目类容器

  1. <?php
  2. namespace _1205;
  3. require 'Maker.php';
  4. require 'Product.php';
  5. require 'Container.php';
  6. class Client2{
  7. public function show(Maker $maker,Product $product){
  8. echo $product->get($maker);
  9. }
  10. }
  11. //绑定到容器
  12. $container = new Container();
  13. $container->bind('maker',function (){return new Maker();});
  14. $container->bind('product',function (){return new Product();});
  15. //实例化
  16. $c2 = new Client2();
  17. $c2->show($container->make('maker'),$container->make('product'));

inc1交通工具

  1. <?php
  2. namespace base\inc1;
  3. class Car{
  4. public function drive(){
  5. return '开汽车';
  6. }
  7. }
  8. class Plane{
  9. public function drive(){
  10. return '乘飞机';
  11. }
  12. }
  13. class Train{
  14. public function drive(){
  15. return '坐火车';
  16. }
  17. }

autoload.php自动加载

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

Travel1.php旅行类,普通写法

  1. <?php
  2. use base\inc1\Car;
  3. use base\inc1\Plane;
  4. use base\inc1\Train;
  5. require __DIR__ .'/autoload.php';
  6. class Travel1{
  7. // 交通工具
  8. private $vehicle;
  9. // 构造方法
  10. public function __construct($vehicle)
  11. {
  12. switch (strtolower($vehicle)) {
  13. case 'car':
  14. $this->vehicle = new Car();
  15. break;
  16. case 'train':
  17. $this->vehicle = new Train();
  18. break;
  19. case 'plane':
  20. $this->vehicle = new Plane();
  21. }
  22. }
  23. // 调用外部一个依赖对象
  24. public function travelModel()
  25. {
  26. return $this->vehicle->drive() . ' : 去旅行';
  27. }
  28. }
  29. // 客户端调用
  30. echo (new Travel1('car'))->travelModel(), '<br>';
  31. echo (new Travel1('train'))->travelModel(), '<br>';
  32. echo (new Travel1('plane'))->travelModel(), '<br>';

Travel2.php旅行类,门面

  1. <?php
  2. use base\inc1\Car;
  3. use base\inc1\Plane;
  4. use base\inc1\Train;
  5. require __DIR__ .'/autoload.php';
  6. class Factory{
  7. public static $instance;
  8. public static function getInstance($vehicle){
  9. switch (strtolower($vehicle)) {
  10. case 'car':
  11. static::$instance = new Car();
  12. break;
  13. case 'train':
  14. static::$instance = new Train();
  15. break;
  16. case 'plane':
  17. static::$instance = new Plane();
  18. }
  19. return static::$instance;
  20. }
  21. }
  22. class Travel2{
  23. // 交通工具
  24. private $vehicle;
  25. // 构造方法
  26. public function __construct($vehicle){
  27. $this->vehicle = Factory::getInstance($vehicle);
  28. }
  29. // 调用外部一个依赖对象
  30. public function travelModel(){
  31. return $this->vehicle->drive() . ' : ~~~~去旅行';
  32. }
  33. }
  34. // 客户端调用
  35. echo (new Travel2('car'))->travelModel(), '<br>';
  36. echo (new Travel2('train'))->travelModel(), '<br>';
  37. echo (new Travel2('plane'))->travelModel(), '<br>';

Travel3.php旅行类,接口

  1. <?php
  2. use base\inc2\Car;
  3. use base\inc2\Plane;
  4. use base\inc2\Train;
  5. use base\inc2\Ship;
  6. use base\inc2\iVehicle;
  7. require __DIR__ .'/autoload.php';
  8. class Travel3{
  9. // 交通工具
  10. private $vehicle;
  11. // 构造方法
  12. public function __construct(iVehicle $vehicle){
  13. $this->vehicle = $vehicle;
  14. }
  15. // 调用外部一个依赖对象
  16. public function travelModel(){
  17. return $this->vehicle->drive() . ' : ******去旅行';
  18. }
  19. }
  20. // 客户端调用
  21. echo (new Travel3(new Car()))->travelModel(), '<br>';
  22. echo (new Travel3(new Train()))->travelModel(), '<br>';
  23. echo (new Travel3(new Plane()))->travelModel(), '<br>';
  24. echo (new Travel3(new Ship()))->travelModel(), '<br>';

iVehicle.php接口

  1. <?php
  2. namespace base\inc2;
  3. interface iVehicle{
  4. public function drive();
  5. }

接口交通工具类

  1. <?php
  2. namespace base\inc2;
  3. class Car implements iVehicle{
  4. public function drive(){
  5. return '开汽车';
  6. }
  7. }
  8. class Plane implements iVehicle{
  9. public function drive(){
  10. return '乘飞机';
  11. }
  12. }
  13. class Ship implements iVehicle{
  14. public function drive(){
  15. return '坐轮船';
  16. }
  17. }
  18. class Train implements iVehicle{
  19. public function drive(){
  20. return '坐火车';
  21. }
  22. }

2.手写课堂笔记:1205.md

总结

今天学习了单列模式:创建类的唯一实例 全局类,工厂模式:批量创建类实例/对象,然后学习了依赖注入,依赖注入就是不要在类的内部new另外一个类,应该在类外传入来降低耦合,在通过容器来学习了将一些重复多余的代码放入到容器中,尽量精简类中的代码。在学习了面向接口,大大的降低程序的耦合性。

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