duandan9680 2017-04-04 01:09
浏览 42
已采纳

在Laravel中实现与Cache :: disk('x')类似的功能

This is just a general question around a solution I'm trying to find.

I have potentially many providers of the same type of service and I need a way to be able to have a default, but then also manually call a switcher method to change them.

Currently, I've bound an Interface to an Implementation via configuration settings and this works well - but it means I can only support one active provider per type of service.

I noticed the Cache::disk() method is really what I'm looking for, but I'm not sure where this type of switch method should be defined.

Current:

interface IProvider {

    public function getServiceInfo($args);

    public function setServiceInfo($args);

}

class GoldService implements IProvider {
    // implementation of the two interface methods

}

class SilverService implements IProvider {

}


// ProviderServiceProvider

public function register()
{
    $this->app->bind(
        App/IProvider,
        App/GoldService
    );
}


// Controller

public function getServiceInfo(Service $serviceId) 
{
    $provider = $app->make('App/IProvider');
    $provider->getServiceInfo($serviceId);
}

Want to have.

// What I want to be able to do
public function getServiceInfo(Service $serviceId)
{
    // Using a facade

    if ($serviceId > 100)
    {
        Provider::getServiceInfo($serviceId);       
    }
    else 
    {
        Provider::switch('SilverService')
            ->getServiceInfo($serviceId);
    }
}

I know I've thrown in an additional requirement there of the Facade - not sure if I've confused Contracts/Facades here - but essentially I want the interface to enforce the instance methods, but Facade for easy access to the instances.

Not really looking for code here - that'll be the easy part. I'm just not sure I've really grok'd this and looking for a nudge in the right direction..

  • 写回答

1条回答 默认 最新

  • dpz1983 2017-04-04 01:20
    关注

    Using an interface to ensure a service implements the methods you require makes sense.

    But with regard to using a different service based on the properties of an object instance; that sounds more like the Factory pattern to me.

    http://www.phptherightway.com/pages/Design-Patterns.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)