1.通过Cache的使用简单了解下laravel中的facade兼谈门面设计模式

larava使用缓存

1.配置和文件位置

laravel中使用默认使用的是文件缓存qvdong驱动,看配置文件

config/cache.php

'default' => env('CACHE_DRIVER', 'file'),

缓存文件的位置是

  'file' => [
            'driver' => 'file',
            'path' => storage_path('framework/cache/data'),
        ],

storage>framework/cache/data

2.简单使用

use Illuminate\Support\Facades\Cache;

Route::get('/cache', function () {
    
    Cache::put('name', 'king',1);
    echo Cache::get("name");

});
  1. 存储:put(key ,value ,minutes) minutes是过去的shijian时间,分钟
  2. 读取:get(key)

缓存类的使用

通过命名空间 我们可以看到 缓存类在Illuminate\Support\Facades\Cache;下面.我们去到这个类

<?php

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Cache\CacheManager
 * @see \Illuminate\Cache\Repository
 */
class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'cache';
    }
}

这个类里面,并没有put个get两个静态方法,我们看父类Facade

Fache里面 也没有put和get静态方法。没有这个方法,为什么可以调用呢,这时候我们就知道在php里面有个魔术方法

 __callStatic():当访问不存在的静态方法名称时,此方法会被自动调用。

1. 调用示例:public static function __callStatic($name,$argument){}  此方法为静态方法(static)

2. 注意:访问控制关键字必须为public;必须有两个参数:对象访问的方法名称($name)、方法包含的参数($argument,数组);

我们看到Facde类里面有这个方法

  /**
     * Handle dynamic, static calls to the object.
     *
     * @param  string  $method
     * @param  array   $args
     * @return mixed
     *
     * @throws \RuntimeException
     */
    public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();

        if (! $instance) {
            throw new RuntimeException('A facade root has not been set.');
        }

        return $instance->$method(...$args);
    }

这个方法里面,我们使用静态的方式获得一个对象,并调用里面的方法。

对象怎么获取

 /**
     * Get the root object behind the facade.
     *
     * @return mixed
     */
    public static function getFacadeRoot()
    {
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    }

这个类调用了 getFacadeAccessor 这个方法在Cache里面 ,他返回的是个字符串


/**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'cache';
    }

我们通过解析这个字符串,获得对应的类对象

 /**
     * Resolve the facade root instance from the container.
     *
     * @param  string|object  $name
     * @return mixed
     */
    protected static function resolveFacadeInstance($name)
    {
        if (is_object($name)) {
            return $name;
        }

        if (isset(static::$resolvedInstance[$name])) {
            return static::$resolvedInstance[$name];
        }

        return static::$resolvedInstance[$name] = static::$app[$name];
    }

至于对象怎么存储进去的,这里占时不先分析了。

我们现在知道了,但我们访问get和put的时候,使用的是静态的形式,但他不是某个类中的静态方法,而是对应某个对象的非静态成员方法。

  public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();

        if (! $instance) {
            throw new RuntimeException('A facade root has not been set.');
        }

        return $instance->$method(...$args);
    }

Facade的真正作用

所以我们可以看到Laravel中Facade的真正作用是避免使用new关键字实例化类,而是通过一个假静态方法最快的使用某一个类中的某个方法。

门面模式Facade

读音是[fəˈsɑːd],源自法语 façade,法语这个词原意就是frontage,意思是建筑的正面,门面,由于以前法国,意大利的建筑只注重修葺临街的一面,十分精美,而背后却比较简陋,所以这个词引申的意思是表象,假象。

在设计模式中,其实Facade这个概念十分简单。

它主要讲的是设计一个接口来统领所有子系统的功能,屏蔽复制的逻辑,暴露一个简单的接口给用户,用户不必关心背后的逻辑。看完下面这个电脑启动的例子

class CPU{
    public function freeze() { ... };
    public function jump() { ... };
    public function execute() { ... };
}
class HardDrive {
    public function read($boot_sector, $sector_size){ ... }
}
class Memory {
    public function load($boot_address, $hd_data) { ... }
}

这是三个电脑中的子系统,我们需要写一个总系统来组织它们之间的关系,这其实就是Facade:

class ComputerFacade {
    private $cpu;
    private $ram;
    private $hd;
    public function __construct() {
        $this->cpu = new CPU();
        $this->ram = new Memory();
        $this->hd = new HardDrive();
    }
    public function start() {
        $this->cpu->freeze();
        $this->ram->load(BOOT_ADDRESS, $this->hd->read(BOOT_SECTOR, SECTOR_SIZE));
        $this->cpu->jump(BOOT_ADDRESS);
        $this->cpu->execute();
    }
}

使用:

$computer = new ComputerFacade();
$computer->start();

门面模式其实就是这么回事,由一个门面(入口)把所有子系统隐藏起来了,只需要操作门面就可以。

总结

可以看出Laravel中的Facade和真正的门面模式还是有点差别,但他们都都隐藏内部实现,只暴露简单的对外接口给使用者。这种设计方便了使用者。

转载于:https://my.oschina.net/2016jyh/blog/2994586

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值