For me it gets down to the real utility of Factories, and that is
intelligent object creation. A Factory makes sense when there is a lot
of duplicated instantiation code or initialization/decision complexity.
I think Factories get overused -- especially in cases like Zend_Db where
the instantiation of individual adapters is actually very simple.
The only problem right now is that Zend_Db has all the constants in it,
but they could be moved to the Abstract class. You could leave Zend_Db
as a trivial Factory for those who wanted it.
As for an Adapter, I guess the question becomes, why do:
$db = new Zend_Db(new Zend_Db_Pdo_Mysql($config));
when there is not much (if anything) to adapt. You could just do:
$db = new Zend_Db_Pdo_Mysql($config);
This is where I think Service Locator is of benefit to frameworks
because it is a natural reducer of the use of specific Factories where
they are not appropriate.
Gavin Vess wrote:
The ZF team wants to push for a conclusion with factories:
http://framework.zend.com/wiki/display/ZFDEV/Factories
Personally, I completely agree with you Chris, and even suggested
removing Zend_Db. For me, whatever effort is required to map input
choice selections onto selected plugin strings (first parameter of the
factory method) is not better than the effort to require_once the
adapter and then instantiate it.
Cheers,
Gavin
Christopher Thompson wrote:
I continue to wonder at the usage of statics. The use case where a
factory might be needed was give as "configure the data source via
GUI." But what is the real difference between:
$dbconfig = "Zend_Db_Adapter_Pdo_Mysql"
$db = new $dbconfig(...)
and
$dbconfig = Zend_Db::PDO_MYSQL;
$db = new Zend_Db($dbconfig, ...)
From my point of view, all the factory provides is more code to
support and more overhead to create an object. I never liked this
about PEAR::DB either.
Matthew Ratzloff wrote:
By class constants being a bit ugly, you mean like this?
$db = new Zend_Db(Zend_Db::PDO_MYSQL, ...)
Would this allow people to use their own adapters without having to
extend
Zend_Db purely to add another constant? The only way I can see that it
wouldn't is if it simply contained the string
"Zend_Db_Adapter_Pdo_Mysql".
It might not be particularly important for Zend_Db since most people
will
be fine with the bundled adapters, but since we're going for a
consistant
approach across all classes we need to consider how something like this
would work in all cases.
-Matt