I would like to:
- Make public services visible to all classes that need them.
- with a minimal template and
- without sacrificing verification!
This is a small project, and I think that DI might be redundant, but maybe I'm wrong? Anyway, I focused on the ServiceLocator template as described by Martin Fowler
In the client class constructor, I have something like this:
this->db = Locator::getDb();
this->log = Locator::getLogger();
Then the other methods of the class access the service through these member attributes, for example:
this->fooModel = new fooModel(this->db);
fooItem1234 = this->fooModel->findById(1234);
However, I will also like this level of visibility for "model" objects (for example, fooModel above), because they are accessed from several different places, and there is no need to have more than one instance.
, , Locator, ::getFooModel(), , Open/Closed, Locator , .
OCP, Dynamic Service Locator ( Fowler), , , .. .
. :
fooItem1234 = FooModel::findById(1234);
, . . Locator, , , . -, - , fooModels , / . , , .
, DI. , , , .
: StackOverflow!