Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:单例, 不是单列
demo1.php
单例模式
<?php
namespace _1205;
//单列模式
class Demo1{
//将构造函数设置为私有
private function __construct()
{
}
//定义单列成员
private static $instance;
//外部调用静态方法
public static function init(){
//判断是否为空
if (is_null(static::$instance)){
//为空创建
static::$instance = new self();
}
//不为空返回
return static::$instance;
}
//禁用克隆方法,防止外部通过克隆方法实例化
private function __clone()
{
}
}
//实例化
$d1 = Demo1::init();
$d2 = Demo1::init();
var_dump($d1,$d2);
echo '<hr>';
use PDO;
//单列pdo类
class Db{
//单列pdo成员
private static $pdo;
//禁用构造函数
private function __construct($connectParas=[])
{
static::$pdo = new PDO($connectParas[0],$connectParas[1],$connectParas[2]);
}
public static function init($connectParas=[]){
if (is_null(static::$pdo)){
new self($connectParas);
}
return static::$pdo;
}
//禁用克隆方法
private function __clone()
{
}
}
$db1 = Db::init(['mysql:host=localhost;dbname=jason','root','root']);
var_dump($db1);
$result = $db1->query('select * from `account`',PDO::FETCH_ASSOC);
foreach ($result as $row){
echo '<pre>' . print_r($row, true) . '</pre>';
}
demo2.php
工厂模式
<?php
//工厂方法
namespace _1205;
use mvc\Facade;
class Test1{
public function __construct($a1){
echo '对象创建成功,参数是'.$a1.'<hr>';
}
}
class Test2{
public function __construct($a1,$a2){
echo '对象创建成功,参数是'.implode(',',[$a1,$a2]) .'<hr>';
}
}
class Test3{
public function __construct($a1,$a2,$a3){
echo '对象创建成功,参数是'.implode(',',[$a1,$a2,$a3]).'<hr>';
}
}
class Factory{
public static function create($className,...$argments){
return new $className(...$argments);
}
}
Factory::create(Test1::class,100);
Factory::create(Test2::class,100,200);
Factory::create(Test3::class,100,200,300);
demo3.php
注入复习
<?php
namespace _1205;
//普通写法
//人类
class Person{
//创建车成员
public $car = null;
public function __construct()
{
$this->car = new Car();
}
public function make(){
$this->car->drive();
}
}
//车类
class Car{
public function drive(){
echo '开车去上班';
}
}
//实例化
$p1 = new Person();
$p1->make();
echo '<hr>';
//依赖注入
class Person1{
//创建车成员
public $car1 = null;
public function __construct(Car1 $car1)
{
$this->car1 = $car1;
}
public function make(){
$this->car1->drive();
}
}
//车类
class Car1{
public function drive(){
echo '开车去上班~~~~';
}
}
//实例化
$car1 = new Car1();
$p2 = new Person1($car1);
$p2->make();
Container.php
容器
<?php
namespace _1205;
class Container{
public $instances = [];
public function bind($alias,\Closure $closure){
$this->instances[$alias] = $closure;
}
public function make($alias,$paras=[]){
return $this->instances[$alias]();
}
}
Maker.php
制作类
<?php
namespace _1205;
class Maker{
public function get(){
return '苹果';
}
}
Product.php
产品类
<?php
namespace _1205;
// 商品类
class Product
{
public function get(Maker $maker)
{
return '该手机是由: <span style="color:orangered;font-weight: bolder">' . $maker->get() . ' </span>生产的';
}
}
Client1.php
项目类
<?php
namespace _1205;
require 'Product.php';
require 'Maker.php';
class Client1{
public function show(){
//创建生产类
$maker = new Maker();
//创建
$product = new Product();
echo $product->get($maker);
}
}
$c1 = new Client1();
$c1->show();
Client2.php
项目类容器
<?php
namespace _1205;
require 'Maker.php';
require 'Product.php';
require 'Container.php';
class Client2{
public function show(Maker $maker,Product $product){
echo $product->get($maker);
}
}
//绑定到容器
$container = new Container();
$container->bind('maker',function (){return new Maker();});
$container->bind('product',function (){return new Product();});
//实例化
$c2 = new Client2();
$c2->show($container->make('maker'),$container->make('product'));
inc1
交通工具
<?php
namespace base\inc1;
class Car{
public function drive(){
return '开汽车';
}
}
class Plane{
public function drive(){
return '乘飞机';
}
}
class Train{
public function drive(){
return '坐火车';
}
}
autoload.php
自动加载
<?php
spl_autoload_register(function ($className){
$path = str_replace('\\', '/', $className);
require dirname(__DIR__) . DIRECTORY_SEPARATOR . $path . '.php';
});
Travel1.php
旅行类,普通写法
<?php
use base\inc1\Car;
use base\inc1\Plane;
use base\inc1\Train;
require __DIR__ .'/autoload.php';
class Travel1{
// 交通工具
private $vehicle;
// 构造方法
public function __construct($vehicle)
{
switch (strtolower($vehicle)) {
case 'car':
$this->vehicle = new Car();
break;
case 'train':
$this->vehicle = new Train();
break;
case 'plane':
$this->vehicle = new Plane();
}
}
// 调用外部一个依赖对象
public function travelModel()
{
return $this->vehicle->drive() . ' : 去旅行';
}
}
// 客户端调用
echo (new Travel1('car'))->travelModel(), '<br>';
echo (new Travel1('train'))->travelModel(), '<br>';
echo (new Travel1('plane'))->travelModel(), '<br>';
Travel2.php
旅行类,门面
<?php
use base\inc1\Car;
use base\inc1\Plane;
use base\inc1\Train;
require __DIR__ .'/autoload.php';
class Factory{
public static $instance;
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 Travel2{
// 交通工具
private $vehicle;
// 构造方法
public function __construct($vehicle){
$this->vehicle = Factory::getInstance($vehicle);
}
// 调用外部一个依赖对象
public function travelModel(){
return $this->vehicle->drive() . ' : ~~~~去旅行';
}
}
// 客户端调用
echo (new Travel2('car'))->travelModel(), '<br>';
echo (new Travel2('train'))->travelModel(), '<br>';
echo (new Travel2('plane'))->travelModel(), '<br>';
Travel3.php
旅行类,接口
<?php
use base\inc2\Car;
use base\inc2\Plane;
use base\inc2\Train;
use base\inc2\Ship;
use base\inc2\iVehicle;
require __DIR__ .'/autoload.php';
class Travel3{
// 交通工具
private $vehicle;
// 构造方法
public function __construct(iVehicle $vehicle){
$this->vehicle = $vehicle;
}
// 调用外部一个依赖对象
public function travelModel(){
return $this->vehicle->drive() . ' : ******去旅行';
}
}
// 客户端调用
echo (new Travel3(new Car()))->travelModel(), '<br>';
echo (new Travel3(new Train()))->travelModel(), '<br>';
echo (new Travel3(new Plane()))->travelModel(), '<br>';
echo (new Travel3(new Ship()))->travelModel(), '<br>';
iVehicle.php
接口
<?php
namespace base\inc2;
interface iVehicle{
public function drive();
}
接口交通工具类
<?php
namespace base\inc2;
class Car implements iVehicle{
public function drive(){
return '开汽车';
}
}
class Plane implements iVehicle{
public function drive(){
return '乘飞机';
}
}
class Ship implements iVehicle{
public function drive(){
return '坐轮船';
}
}
class Train implements iVehicle{
public function drive(){
return '坐火车';
}
}
今天学习了单列模式:创建类的唯一实例 全局类,工厂模式:批量创建类实例/对象,然后学习了依赖注入,依赖注入就是不要在类的内部new另外一个类,应该在类外传入来降低耦合,在通过容器来学习了将一些重复多余的代码放入到容器中,尽量精简类中的代码。在学习了面向接口,大大的降低程序的耦合性。