dart_in_action/40_design_pattern_front_con...

81 lines
1.9 KiB
Dart
Raw Permalink Normal View History

2019-03-27 10:55:27 +08:00
/**
Front Controller Pattern
Front Controller - web的应用程序web的应用程序
Dispatcher - 使
View -
*/
main(List<String> args) {
// 用来提供一个集中的请求处理机制,所有的请求都将由一个单一的处理程序处理。
// 该处理程序可以做认证/授权/记录日志,或者跟踪请求,然后把请求传给相应的处理程序
FrontController frontController = new FrontController();
frontController.dispatchRequest("HOME");
frontController.dispatchRequest("STUDENT");
}
//////////////////////////////////////////////////////////////////
///
/// 创建视图
///
class HomeView {
void show() {
print("Displaying Home Page");
}
}
class StudentView {
void show() {
print("Displaying Student Page");
}
}
///
/// 创建调度器 Dispatcher
///
class Dispatcher {
StudentView _studentView;
HomeView _homeView;
Dispatcher() {
_studentView = StudentView();
_homeView = HomeView();
}
void dispatch(String request) {
if (request.toUpperCase() == "STUDENT") {
_studentView.show();
} else {
_homeView.show();
}
}
}
///
/// 创建前端控制器 FrontController
///
class FrontController {
Dispatcher _dispatcher;
FrontController() {
_dispatcher = Dispatcher();
}
bool _isAuthenticUser() {
print("User is authenticated successfully.");
return true;
}
void _trackRequest(String request) {
print("Page requested: " + request);
}
void dispatchRequest(String request) {
//记录每一个请求
_trackRequest(request);
//对用户进行身份验证
if (_isAuthenticUser()) {
_dispatcher.dispatch(request);
}
}
}