0

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..

Trent
  • 2,909
  • 1
  • 31
  • 46

1 Answers1

0

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

fubar
  • 16,918
  • 4
  • 37
  • 43
  • So I would drop the interface binding, and implement a factory class which instantiates whatever service I want to use (and has a default in config)? I then facade the factory class? – Trent Apr 04 '17 at 02:07
  • I've marked this as answered because it pushed me in the right direction - but my implementation looks sloppy, so I'll create a new question :) – Trent Apr 04 '17 at 05:37
  • Include a link to the question here. – fubar Apr 04 '17 at 05:38
  • Here it is http://stackoverflow.com/questions/43200227/implementation-of-facade-in-front-of-factory-class-laravel-5-4 – Trent Apr 04 '17 at 06:52