三十四、前端控制器模式(Front Controller Pattern)
创始人
2024-05-15 20:43:00
0

前端控制器模式(Front Controller Pattern)是用来提供一个集中的请求处理机制,所有的请求都将由一个单一的处理程序处理

该处理程序可以做认证/授权/记录日志,或者跟踪请求,然后把请求传给相应的处理程序

前端控制器模式涉及以下实体

  • 前端控制器(Front Controller) - 处理应用程序所有类型请求的单个处理程序,应用程序可以是基于 web 的应用程序,也可以是基于桌面的应用程序。
  • 调度器(Dispatcher) - 前端控制器可能使用一个调度器对象来调度请求到相应的具体处理程序。
  • 视图(View) - 视图是为请求而创建的对象。

实现

1、 定义类 FrontController 、 Dispatcher 分别当作前端控制器和调度器
2、 定义类 HomeView 和 StudentView 表示作为前端控制器接收到的请求而创建的视图
3、 定义类 FrontControllerPatternDemo 使用 FrontController 演示前端控制器设计模式

范例

1. 创建视图

HomeView.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.package com.souyunku.tech.gof;public class HomeView {public void show(){System.out.println("Displaying Home Page");}
}

StudentView.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.package com.souyunku.tech.gof;public class StudentView {public void show(){System.out.println("Displaying Student Page");}
}

2. 创建调度器 Dispatcher

Dispatcher.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.package com.souyunku.tech.gof;public 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. 创建前端控制器 FrontController

Context.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.package com.souyunku.tech.gof;public 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.java

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.package com.souyunku.tech.gof;public class FrontControllerPatternDemo {public static void main(String[] args) {FrontController frontController = new FrontController();frontController.dispatchRequest("HOME");frontController.dispatchRequest("STUDENT");}
}

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com/souyunku/tech/gof/FrontControllerPatternDemo.java
$ java  com.souyunku.tech.gof.FrontControllerPatternDemo
Page requested: HOME
User is authenticated successfully.
Displaying Home Page
Page requested: STUDENT
User is authenticated successfully.
Displaying Student Page

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...