[Refactored] Would you refactor this to involve a Factory?
Posted: Thu Aug 25, 2005 10:09 pm
Here's my evolving FrontDispatcher
The simple Mapping array is about to be replaces -- > request templates need to map to a module name AND to a handler chain. The last handler in the chain will be the current ModuleController. (Ie I want to add a DBUnavailableHandler, BadCupHandler before the immediate dispatch to the module controller (which has its own set of mappings->CoRs)
However the collection of _createModuleFoo's is making me wonder if I should pull those three out to a Factory? They already are FactoryMethods.... would you make a Factory class for them to keep the dispatcher only worried about dispatching?
Code: Select all
class FrontDispatcher {
var $_mappings=array();
function FrontDispatcher() {
$this->_mappings["|register/[-A-Za-z0-9_]*/Admin|"]="SlidingDoorsAdmin";
}
function _findModule() {
foreach($this->_mappings as $pattern=>$module)
if (preg_match($pattern,$_SERVER["REQUEST_URI"]))
return $module;
return "";
}
function _createModuleController($className) {
$className.="Controller";
require_once("classes/controllers/$className.inc");
$mc =& new $className();
return $mc;
}
function _createModuleRequest($className) {
$className.="Request";
require_once("classes/requests/$className.inc");
$req =& new $className();
return $req;
}
function _createModuleContext($className) {
$className.="Context";
require_once("classes/contexts/$className.inc");
$con =& new $className();
return $con;
}
function dispatch() {
$moduleName = $this->_findModule();
if ($moduleName!="") {
$req =& $this->_createModuleRequest($moduleName);
$con =& $this->_createModuleContext($moduleName);
$mc =& $this->_createModuleController($moduleName);
$mc->invoke($req,$con);
return 1;
}
else return 0;
}
}
?>However the collection of _createModuleFoo's is making me wonder if I should pull those three out to a Factory? They already are FactoryMethods.... would you make a Factory class for them to keep the dispatcher only worried about dispatching?