I use Zend Framework (the MVC), but the question is generic. In my system, I have several layers of abstraction: mappers, services (or repositories), view scripts, the objects. The problem is that depending on the type of objects, the factories for mappers, services, and so on have to instantiate specific mappers, services, etc. So in the factories, I use the same if
condition but outputting different object, for example:
if ('type1' == $objectType) {
$mapper = new DbMapperForObjectType1();
} elseif ('type2' == $objectType) {
$mapper = new DbMapperForObjectType2();
}
// etc.
return $mapper;
The same goes for services and other things.
I wonder if there's a design pattern or solution to put these if
's for different things into one place to avoid repeating them in different factories.