美文网首页
前端控制器模式

前端控制器模式

作者: 散装咖啡 | 来源:发表于2017-05-30 02:55 被阅读49次
    //frontController
    class HomeView
    {
        public function show()
        {
            echo "Displaying Home Page". "<br />";
        }
    }
    
    class StudentView
    {
        public function show()
        {
            echo "Displaying Student Page". "<br />";
        }
    }
    
    class Dispatcher
    {
        private $studentView = null;
        private $homeView = null;
        
        public function __construct()
        {
            $this->studentView = new StudentView();
            $this->homeView = new HomeView();
        }
        
        public function dispatch($request)
        {
            //返回0就是相等
            if (!strcasecmp('STUDENT', $request)) {
                $this->studentView->show();
            } else {
                $this->homeView->show();
            }
        }
    }
    
    class FrontController
    {
        private $dispatcher = null;
        public function __construct()
        {
            $this->dispatcher = new Dispatcher();
        }
        public function isAuthenticUser(){
            echo "User is authenticated successfully.";
            return true;
        }
        public function trackRequest($request)
        {
            echo "Page requested: " . $request;
        }
        
        public function dispatchRequest($request)
        {
            //记录每一个请求
            $this->trackRequest($request);
            //对用户进行身份验证
          if($this->isAuthenticUser()){
             $this->dispatcher->dispatch($request);
          }
        }
    }
    
    $FrontController = new FrontController();
    $FrontController->dispatchRequest("HOME");
    $FrontController->dispatchRequest("STUDENT");
    

    参考文章 http://www.runoob.com/design-pattern/front-controller-pattern.html

    相关文章

      网友评论

          本文标题:前端控制器模式

          本文链接:https://www.haomeiwen.com/subject/yrglfxtx.html