日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

php 前端控制器,前端控制器模式

發(fā)布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 前端控制器,前端控制器模式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前端控制器模式(Front Controller Pattern)是用來提供一個集中的請求處理機制,所有的請求都將由一個單一的處理程序處理。該處理程序可以做認證/授權/記錄日志,或者跟蹤請求,然后把請求傳給相應的處理程序。以下是這種設計模式的實體。前端控制器(Front Controller) - 處理應用程序所有類型請求的單個處理程序,應用程序可以是基于 web 的應用程序,也可以是基于桌面的應用程序。

調(diào)度器(Dispatcher) - 前端控制器可能使用一個調(diào)度器對象來調(diào)度請求到相應的具體處理程序。

視圖(View) - 視圖是為請求而創(chuàng)建的對象。

實現(xiàn)

我們將創(chuàng)建 FrontController、Dispatcher 分別當作前端控制器和調(diào)度器。HomeView 和 StudentView 表示各種為前端控制器接收到的請求而創(chuàng)建的視圖。

FrontControllerPatternDemo,我們的演示類使用 FrontController 來演示前端控制器設計模式。

步驟 1

創(chuàng)建視圖。

HomeView.javapublic?class?HomeView?{

public?void?show(){

System.out.println("Displaying?Home?Page");

}

}

StudentView.javapublic?class?StudentView?{

public?void?show(){

System.out.println("Displaying?Student?Page");

}

}

步驟 2

創(chuàng)建調(diào)度器 Dispatcher。

Dispatcher.javapublic?class?Dispatcher?{

private?StudentView?studentView;

private?HomeView?homeView;

public?Dispatcher(){

studentView?=?new?StudentView();

homeView?=?new?HomeView();

}

public?void?dispatch(String?request){

if(request.equalsIgnoreCase("STUDENT")){

studentView.show();

}else{

homeView.show();

}

}

}

步驟 3

創(chuàng)建前端控制器 FrontController。

Context.javapublic?class?FrontController?{

private?Dispatcher?dispatcher;

public?FrontController(){

dispatcher?=?new?Dispatcher();

}

private?boolean?isAuthenticUser(){

System.out.println("User?is?authenticated?successfully.");

return?true;

}

private?void?trackRequest(String?request){

System.out.println("Page?requested:?"?+?request);

}

public?void?dispatchRequest(String?request){

//記錄每一個請求

trackRequest(request);

//對用戶進行身份驗證

if(isAuthenticUser()){

dispatcher.dispatch(request);

}

}

}

步驟 4

使用 FrontController 來演示前端控制器設計模式。

FrontControllerPatternDemo.javapublic?class?FrontControllerPatternDemo?{

public?static?void?main(String[]?args)?{

FrontController?frontController?=?new?FrontController();

frontController.dispatchRequest("HOME");

frontController.dispatchRequest("STUDENT");

}

}

步驟 5

驗證輸出。Page?requested:?HOME

User?is?authenticated?successfully.

Displaying?Home?Page

Page?requested:?STUDENT

User?is?authenticated?successfully.

Displaying?Student?Page

總結(jié)

以上是生活随笔為你收集整理的php 前端控制器,前端控制器模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。