T
T
thorii2016-10-04 17:19:44
PHP
thorii, 2016-10-04 17:19:44

Interface vs Provider, is there a difference in the implementation of the functionality described below?

Let's take the caching class as an example.
We need to write functionality for several caching engines (Redis, File, SQLite).
On the one hand, you can write a CacheProvider that describes common methods for caching (get, set) and connects the necessary "drivers" through named constructors.
This is how I see it being used:

$cache= CacheProvider::redis(array $cfg) //@return Redis instance
//Или
$cache = CacheProvider::driver(string CacheProvider::DRIVER_REDIS, array $cfg);

Cons I see:
  • When creating a new driver, you need to update the Cache class and add a new constructor (In the case of "Or" - a constant)
  • Track so that the number of constructors is equal to the number of drivers (in the case of "Or" - constants)
  • There will be an additional dependency on the CacheProvider middleware class

On the other hand, you can write an interface and implement it directly in the RedisCache, FileCache classes
Usage:
$cache = new RedisCache(array $cfg);I don't see any disadvantages in this method yet. If they are still there, open my eyes. Googled Interface vs Provider But did not find a clear one.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question