Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:抽象方法也并非没有缺点, 用哪个方式来创建类实例, 还是要看具体场景的要求
单例模式,就是不允许过多的实例化一个类,例如在连接数据库的时候,数据库的连接有最大上限1000,NEW一次PDO就会多一个类实例,前面的连接如果没释放,就会增加一个,达到最大连接数以后,后面的就不无法再连接数据库了
例
<?php
$pdo = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
$pdo1 = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
$pdo2 = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
var_dump($pdo);
echo '<br>';
var_dump($pdo1);
echo '<br>';
var_dump($pdo2);
图:3个PDO对象,ID增加到了3
单例例子
<?php
class Db
{
private static $pdo;
//创建一个私有的构造方法,防止外部实例化
private function __construct(...$linkparams)
{
list($dsn,$username,$pwd) = $linkparams;
self::$pdo = new PDO($dsn,$username,$pwd);
}
public static function getPDO(...$linkparams)
{
//检查PDO这个变量有没有被赋值,如果被赋值了就直接返回
if(is_null(self::$pdo)){
new self(...$linkparams);
};
return self::$pdo;
}
//防止克隆当前对象
private function __clone()
{
}
}
$linkparams = ['mysql:host=localhost;dbname=phpedu','root','root'];
$pdo3 = DB::getPDO(...$linkparams);
var_dump($pdo3);
echo '<br>';
$pdo4 = DB::getPDO(...$linkparams);
var_dump($pdo4);
echo '<br>';
$pdo5 = DB::getPDO(...$linkparams);
var_dump($pdo5);
图:不管创建几个,它都是同一个ID,不会增加
<?php
//工厂模式,就是把一些流程分为多个步骤/理解为多个工厂部门也可以,然后由一个工厂类来区别/通知某个类运作
class Car
{
public function Engine()
{
return '汽车引擎';
}
public function Tyre()
{
return '汽车车轮';
}
public function Body()
{
return '汽车车身';
}
}
class Bus
{
public function Engine()
{
return '巴士引擎';
}
public function Tyre()
{
return '巴士车轮';
}
public function Body()
{
return '巴士车身';
}
}
// 工厂模式1
class Factory
{
private static $model;
public static function getModel(string $model)
{
switch ($model) {
case 'car':
self::$model = new Car();
break;
case 'bus':
self::$model = new Bus();
break;
default:
die('错误的车型');
}
return self::$model;
}
}
// 工厂模式2
class Factory2
{
private static $class;
public static function getModel($class)
{
self::$class = new $class();
return self::$class;
}
}
// 工厂模式3
class Factory3
{
private $model;
private $department;
public function __construct($class,$department)
{
$this->department = $department;
$this->model = Factory2::getModel($class);
}
public function getModel()
{
switch($this->department){
case 'engine':
return $this->model->Engine();
break;
case 'body':
return $this->model->Body();
break;
case 'tyre':
return $this->model->Tyre();
break;
}
}
}
$car = Factory::getModel('car');
echo '模式1'. $car->Body().'<hr>';
$car1 = Factory2::getModel('car');
echo '模式2'. $car1->Body().'<hr>';
$bus1 = Factory2::getModel('bus');
echo '模式2'. $bus1->Engine().'<hr>';
$car = new Factory3('car','engine');
echo '模式3'.$car->getModel().'<hr>';
$car = new Factory3('car','body');
echo '模式3'.$car->getModel();
图
<?php
//抽象工厂 区分更加细化,而且可以关闭其中的任意一个接口,而不会影响到整体
interface Engine
{
public function Engine();
}
class CarEngine implements Engine
{
public function Engine()
{
return '汽车引擎';
}
}
class BusEngine implements Engine
{
public function Engine()
{
return '巴士引擎';
}
}
interface Tyre
{
public function Tyre();
}
class CarTyre implements Tyre
{
public function Tyre()
{
return '汽车轮胎';
}
}
class BusTyre implements Tyre
{
public function Tyre()
{
return '巴士轮胎';
}
}
interface Body
{
public function Body();
}
// class CarBody implements Body
// {
// public function Body()
// {
// return '汽车车身';
// }
// }
class BusBody implements Body
{
public function Body()
{
return '巴士车身';
}
}
class Factory
{
private static $model;
public static function getModel($model,$part)
{
if ($model === 'car') {
switch ($part) {
case 'engine':
self::$model = new CarEngine();
break;
case 'tyre':
self::$model = new CarTyre();
break;
case 'body':
self::$model = new CarBody();
break;
}
} elseif ($model === 'bus') {
switch ($part) {
case 'engine':
self::$model = new BusEngine();
break;
case 'tyre':
self::$model = new BusTyre();
break;
case 'body':
self::$model = new BusBody();
break;
}
}
return self::$model;
}
}
class A
{
private $model;
private $part;
public function __construct($model,$part)
{
$this->part = $part;
$this->model = Factory::getModel($model,$part);
}
public function getPart()
{
switch ($this->part) {
case 'engine':
return $this->model->Engine();
break;
case 'tyre':
return $this->model->tyre();
break;
case 'body':
return $this->model->body();
break;
}
}
}
$car = new A('car','engine');
echo $car->getPart().'<hr>';
// $car = new A('car','body');
echo $car->getPart().'<hr>';
$car = new A('bus','engine');
echo $car->getPart().'<hr>';
$car = new A('bus','body');
echo $car->getPart().'<hr>';
图