This mode consists of the command class, the user request data class, the business logic class, the command class factory class and the calling class, the functions of each class are summarized as follows:
1, Command class: Call the user request data class and business logic class;
2, the user requests the data class: Obtains the user request data and saves the background processing to return the result;
3, business logic class: such as the following example to verify the user login information is correct function, etc.;
4, the Command factory class (I take the name, haha): To generate an instance of the command class, this class for the first time I feel a bit of a dick, of course, looked at a few times or feel very dick:);
5. Call class: Invoke command class, generate view;
Look directly at the code:
//Command ClassAbstract classCommand {Abstract functionExecute (commandcontext$context);}classLogincommandextendscommand{//command class for handling user login informationfunctionExecute (commandcotext$context{//commandcotext is a class that processes user request data and background feedback data$manager= Registry::Getaccessmanager (); There is no specific implementation in the original code, but it shows that this is a business logic class for handling user login information .$user=$context->get (' username '); $pass=$context->get (' Pass ')); $user _obj=$manager->login ($user,$pass); if(Is_null($user _obj)) {$context->seterror ($manager-geterror); return false; }$context->addparam (' User ',$user _obj); return true; User login successfully returned true}}classFeedbackcommandextendscommand{//command class for sending messagesfunctionExecute (commandcontext$context) {$msgSystem= Registry::Getmessagesystem (); $email=$context->get (' email '); $msg=$context->get (' msg ')); $topic=$context->get (' Topci '); $result=$msgSystem->send ($email,$msg,$topic); if(!$result) {$context->seterror ($msgSystem-GetError ()); return false; }return true; }}//user request data ClassclassCommandcontext {Private $params=Array(); Private $error= ' '; function__construct () {$this->params =$_request;}functionAddParam ($key,$val) {$this->params[$key] =$val;}functionGet$key) {return $this->params[$key];}functionSetError ($error) {$this->error =$error;}functionGetError () {return $this-error;}}//Command class factory, which generates the command class based on the action in the user request dataclassCommandnotfoundexceptionextends Exception {}classCommandfactory {Private Static $dir= ' Commands '; Static functionGetCommand ($action= ' Default ') {if(Preg_match('/\w ',$action)) {Throw New Exception("Illegal Characters in action"); }$class=Ucfirst(Strtolower($action))." Command "; $file= Self::$dir. Directory_separator. " {$class}.php "; Directory_separator represents '/', which is the path to a command class fileif(!file_exists($file)) {Throw NewCommandnotfoundexception ("Could not find"$file‘"); }require_once($file); if(!class_exists($class)) {Throw NewCommandnotfoundexception ("No"$class' Class located '); }$cmd=New $class(); return $cmd; }}//the caller class, which is the equivalent of a command, it co-ordinates all the resourcesclasscontroller{Private $context; function__construct () {$this->context =NewCommandcontext (); User request Data}functionGetContext () {return $this-context; }functionprocess () {$cmd= Commandfactory::getcommand ($this->context->get (' action '))); To get the command class by using the command factory classif(!$COMD->execute ($this-context)) {//processing failed}Else{//successful//distribution view }}}//Client$controller=NewController ();//Forge user requests, these parameters in real-world scenarios should be obtained by post or get, seemingly nonsense:)$context=$controller-GetContext ();$context->addparam (' action ', ' login ');$context->addparam (' username ', ' bob '));$context->addparam (' Pass ', ' Tiddles '));$controller->process ();
PHP Object-oriented command mode