Blogger Information
Blog 35
fans 3
comment 0
visits 25093
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月5日作业 php 设计模式
随风
Original
687 people have browsed it

将课堂代码全部上机操作

Demo1

`<?php
namespace gzg_05;

// PDO打开全局变量 如不打开 调用时需要 加 \PDO
use PDO;
// 单例模式

// 空类
class Temp{
//…
}

//实例化 两个类
$obj1 = new Temp();
$obj2 = new Temp();
var_dump($obj1,$obj2);
echo ‘<br>‘;

var_dump($obj1 === $obj2);
echo ‘<hr>‘;

// 单例模式原理
class Demo1
{
// 目前 只能在类中的内部将类实例化
// 当前类的实例 用于实例判断
public static $instance = null;

//构造方法私有化
private function __construct()
{
}
// 实例化当前类的方法

  1. public static function getInstance()
  2. {
  3. // 实例化当前类,并返回类实例/对象
  4. // 判断当前类是否已经被实例化过了,如果没有的话就实例化,如果已实例化,就返回它

// self = static 在这里这两个都能用 在场景中用 static 不再用self
if(is_null(self::$instance))
{
// new 一个内部类
self::$instance =new self();
// $demo =new demo();
}

  1. return self::$instance;
  2. }
  3. // 禁用克隆魔术方法
  4. private function __clone()
  5. {
  6. // TODO: Implement __clone() method.
  7. }

}
// 类实例化 静态实例化 Demo:: 实现 公共的 new Demo 实现
$obj3 = Demo1::getInstance();
$obj4 = Demo1::getInstance();
var_dump($obj3,$obj4);
echo ‘<br>‘;
var_dump($obj3 === $obj4);
echo ‘<hr>‘;

// 单例模型应用场景 self 全部变更为static
class Db
{
// 目前 只能在类中的内部将类实例化
// 当前类的实例
public static $pdo = null;

  1. // 构造方法私有化
  2. private function __construct(...$conn)
  3. {
  4. $dsn = $conn[0];
  5. $username =$conn[1];
  6. $password =$conn[2];
  7. static::$pdo = new PDO($dsn,$username,$password);
  8. }
  9. // 实例化当前类的方法 传参使用
  10. public static function getPdo(...$conn)
  11. {
  12. // 实例化当前类,并返回类实例/对象
  13. // 判断当前类是否已经被实例化过了,如果没有的话就实例化,如果已实例化,就返回它
  14. if (is_null(static::$pdo)){
  15. // 构造函数不需要返回
  16. new static(...$conn);
  17. }
  18. return static::$pdo;
  19. }
  20. // 禁用克隆魔术方法
  21. private function __clone()
  22. {
  23. // ...
  24. }

}

$conn = [‘mysql:host=localhost;dbname=gzg’,’root’,’root’];
$pdo = Db::getPdo(…$conn);
print_r($pdo->query(‘SELECT * FROM users‘)->fetchAll());

`

Demo2

`<?php
namespace gzg_05;
// 工厂模式: 用于批量创建类的实例/对象
use mvc\Facade;

class Demo2
{
//….
}
// file1.php
$obj = new Demo2();
// file2.php
$obj = new Demo2();
// file3.php
$obj = new Demo2();

class Test1
{
public function __construct($arg1)
{
echo ‘对象创建成功, 参数是: ‘ . $arg1;
}
}

class Test2
{
public function __construct($arg1, $arg2)
{
echo ‘对象创建成功, 参数是: ‘ . implode(‘, ‘, [$arg1, $arg2]);
}
}

class Test3
{
public function __construct($arg1, $arg2, $arg3)
{
echo ‘对象创建成功, 参数是: ‘ . implode(‘, ‘, [$arg1, $arg2, $arg3]);
}
}
// 工厂类, 专用于创建类实例
class Factory
{
public static function create($className, …$arg)
{
return new $className(…$arg);
}
}

//echo Test1::class; // _1205\Test1: 完整的类名, 带有命名空间的
// 用工厂类来创建类实例/对象
Factory::create(Test1::class,abc);
echo ‘<br>‘;
$test1 = Factory::create(Test1::class,abc);
var_dump($test1);
echo ‘<br>‘;
Factory::create(Test2::class,123,200);
echo ‘<br>‘;
Factory::create(Test3::class,aabb,123,asf);`

Demo3

`<?php
namespace gzg_05;
// 依赖注入: 解决对象调用之间耦合
// 工作类

class Person
{
// 要依赖的外部对象
private $car=null;
// 在构造方法中将依赖的外部对象全部实例化
// 注入点放到构造方法中
public function __construct()
{
$this->car =new Car();
}
// 外部对象执行一个动作
public function work()
{
return $this->car->drive();
}

}
// 依赖的外部类
class Car
{
public function drive()
{
return ‘111111111’;
}
}

// 实例化类
$person = new Person();
echo $person->work();
echo ‘<hr>‘;

/*/
//car = new Car() 在类外调用
class Person1
{
// 要依赖的外部对象
private $car = null;

  1. // 在构造方法中将依赖的外部对象全部实例化
  2. // 注入点放到构造方法中
  3. public function __construct(Car $car)
  4. { //类外调用后传参
  5. $this->car = $car;
  6. }
  7. // 外部对象执行一个动作
  8. public function work()
  9. {
  10. return $this->car->drive();
  11. }

}

// 依赖的外部类
class Car1
{
public function drive()
{
return ‘开车去上班’;
}
}

// 实例化类
//类外实例化
$car = new Car();
//给构造函数传参
$person = new Person1($car);
echo $person->work();
echo ‘<hr>‘;

`

Demo4

file1

`<?php
namespace gzg_05;

//制造商
class Maker
{
public function get ()
{
return’华为’;
}
} <?php

namespace gzg_05;

// 商品类
class Product
{
public function get(Maker $maker)
{
return ‘该手机是由: <span style="color:green;font-weight: bolder">‘ . $maker->get() . ‘ </span>生产的’;
}
} <?php
namespace gzg_05;
//use Closure;

use Closure;

class Container
{
// 类实例容器
protected $instance = [];
// 将类实例化的过程绑定到容器中
public function bind($alias, Closure $process)
{
$this->instance[$alias] = $process;
}

  1. // 取出保存在容器中的实例化过程的闭包,并执行它
  2. public function make($alias)
  3. {
  4. return $this->instance[$alias]();
  5. }

}
<?php
namespace gzg_05;
use http\Client;

require ‘Product.php’;
require ‘Maker.php’;

// 先不用容器
class Client1
{
public function show()
{

  1. $product = new Product();
  2. $make = new Maker();
  3. return $product->get($make);
  4. }

}

$obj = new Client1();
echo $obj->show();
echo ‘<br>‘;

echo ((new Client1())->show());

<?php

namespace gzg_05;

require ‘Product.php’;
require ‘Maker.php’;
require ‘Container.php’;

class Client2
{
// 输出商品与制造商
public function show( Product $product,Maker $maker)
{
// 制造商注入到产品类中
return $product->get($maker);
}
}
// 客户端调用

// 将类实例绑定到容器中并实例化且返回
$container= new Container();
//$container->bind(‘product’,function (){return new Product();});
//$container->bind(‘Maker’,function (){return new Maker();});
$container->bind(‘product’, function () {return new Product();});
$container->bind(‘maker’, function () {return new Maker();});

// 创建类实例并返回
$product = $container->make(‘product’);
$maker = $container->make(‘maker’);
echo (new Client2())->show($product,$maker);
`

Demo4

<?php spl_autoload_register(function ($className){ $path = str_replace('\\', '/', $className); require dirname(__DIR__) . DIRECTORY_SEPARATOR . $path . '.php'; });
`<?php
//旅游类 传统方式
namespace base;
//设置引用的外部类名的别名 把类名全部改为小写 不改别名的话 调用时为 Car 类的实际名称
use base\inc\Car as car;
use base\inc\Train as train;
use base\inc\Plane as plane;
//require DIR . ‘/inc/Car.php’;
//$path = str_replace(‘\‘, ‘/‘, \inc\Car::class);
//echo $path, ‘<br>‘;

require DIR . ‘/autoload.php’;

class Travel10
{
// 交通工具
private $vehicle;
//构造方法
public function __construct($vehicle)
{
// print_r($vehicle);
// exit;
//strtolower() 把参数全部改为小写
// 根据参数进行不同的选择,调用不同的类
switch (strtolower($vehicle)){

  1. case 'car':
  2. $this->vehicle = new Car();
  3. break;
  4. case 'train':
  5. $this->vehicle = new train();
  6. break;
  7. case 'plane':
  8. $this->vehicle = new plane();
  9. }
  10. }
  11. // 调用外部一个依赖对象
  12. public function travelModel()
  13. {
  14. return $this->vehicle->drive() . ' : 去旅行';
  15. }

}

// 客户端调用
// $car = new Travel10(‘car’);
// echo $car->travelModel();
// echo ‘<br>‘;

echo (new Travel10(‘car’))->travelModel(), ‘<br>‘;
//echo ‘111’;

//$car = new Car();
//echo $car->drive();
echo (new Travel10(‘train’))->travelModel(), ‘<br>‘;
echo (new Travel10(‘plane’))->travelModel(), ‘<br>‘; <?php
namespace base;

use base\inc\Train as train;
use base\inc\Car as car;
use base\inc\Plane as plane;

require DIR . ‘/autoload.php’;

//$path = str_replace(‘\‘, ‘/‘, \inc\Car::class);
//echo $path, ‘<br>‘;

// 工厂类, 专用于创建类实例

class Factory1

{
protected static $instance =null;
public static function getInstance($vehicle)
{
switch (strtolower($vehicle))
{
case ‘car’:
static::$instance = new car();
break;
case ‘train’:
static::$instance = new train();
break;
case ‘plane’:
static::$instance = new plane();
}
// 返回当前具体的交通工具
return static::$instance;
}
}

class Travel5
{
// 交通工具
private $vehicle;
//构造方法

  1. public function __construct($vehicle)
  2. {
  3. echo $vehicle;
  4. $this->vehicle = Factory1::getInstance($vehicle);
  5. }
  6. public function travelModel()
  7. {
  8. return $this->vehicle->drive() .'1111111';
  9. }

}

// 客户端调用
$car = new Travel5(‘car’);
echo $car->travelModel();
echo ‘<br>‘;

echo (new Travel5(‘train’))->travelModel(),’<br>‘;

echo (new Travel5(‘plane’))->travelModel(),’<br>‘; <?php

namespace base;

use base\inc1\Car ;
use base\inc1\iVehicle;
use base\inc1\Train;
use base\inc1\Plane;
use base\inc1\Ship;
//
require DIR . ‘/autoload.php’;
//$path = str_replace(‘\‘, ‘/‘, Car1::class);
//echo $path, ‘<br>‘;
//$path = str_replace(‘\‘, ‘/‘, \inc1\iVehicle1::class);
//echo $path, ‘<br>‘;
//exit;

class Travel8
{
// 交通工具
private $vehicle;

  1. // 构造方法
  2. public function __construct(iVehicle $vehicle)
  3. {
  4. $this->vehicle = $vehicle;
  5. }
  6. // 调用外部一个依赖对象
  7. public function travelModel()
  8. {
  9. return $this->vehicle->drive() . ' : ======= 去旅行';
  10. }

}
// $car1 = new Car();
//echo (new Travel8($car))->travelModel(), ‘<br>‘;
//// $car1 = new Trave8($car) ;
//
$car = new Car();
echo (new Travel8($car))->travelModel(), ‘<br>‘;

echo (new Travel8(new Plane()))->travelModel(), ‘<br>‘;

echo (new Travel8(new Ship()))->travelModel(), ‘<br>‘;

$ship1 = new Ship();
$ship2 = new Travel8( $ship1);
echo $ship2->travelModel();
echo ‘<br>‘;

$ship = new Ship();
$ship1 = new Travel8( $ship);
echo $ship1->travelModel();
echo ‘<br>‘;

$ship1 = new Travel8( new $ship);
echo $ship1->travelModel();
echo ‘<br>‘;`

手抄

总结

接口 把面向对象改为面向接口。全部由接口调用
容器 了解了容器的原理,容器就是管理 对象的。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:看来你的markdown语法还没有掌握 , 作业排版问题很多, 赶紧补一下
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