前端控制器模式
2017-05-30 本文已影响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