文章永久连接:https://tech.souyunku.com/2719
前端控制器模式(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
干货推荐
附录:设计模式:系列文章
- 一、设计模式
- 二、设计模式 – 简介
- 三、设计模式 – 四大类型
- 四、设计模式 – 六大原则
- 五、工厂模式 ( Factory Pattern )
- 六、抽象工厂模式 ( Abstract Factory Pattern )
- 七、单例模式 ( Singleton Pattern )
- 八、建造者模式 ( Builder Pattern )
- 九、原型模式 ( Prototype Pattern )
- 十、适配器模式 ( Adapter Pattern )
- 十一、桥接模式 ( Bridge Pattern )
- 十二、过滤器模式 ( Filter Pattern )
- 十三、组合模式 ( Composite Pattern )
- 十四、装饰器模式 ( Decorator Pattern )
- 十五、外观模式 ( Facade Pattern )
- 十六、享元模式 ( Flyweight Pattern )
- 十七、代理模式 ( Proxy Pattern )
- 十八、责任链模式 ( Chain of Responsibility)
- 十九、命令模式 ( Command Pattern )
- 二十、解释器模式 ( Interpreter Pattern )
- 二十一、迭代器模式 ( Iterator Pattern )
- 二十二、中介者模式 ( Mediator Pattern )
- 二十三、备忘录模式 ( Memento Pattern )
- 二十四、观察者模式 ( Observer Pattern )
- 二十五、状态模式 ( State Pattern )
- 二十六、空对象模式 ( Null Object Pattern )
- 二十七、策略模式 ( Strategy Pattern )
- 二十八、模板模式 ( Template Pattern )
- 二十九、访问者模式 ( Visitor Pattern )
- 三十、MVC 模式
- 三十一、业务代表模式(Business Delegate Pattern)
- 三十二、组合实体模式 (Composite Entity Pattern)
- 三十三、数据访问对象模式 ( Data Access Object )
- 【当前读到】三十四、前端控制器模式(Front Controller Pattern)
- 三十五、拦截过滤器模式 ( Intercepting Filter )
- 三十六、服务定位器模式 (Service Locator Pattern)
- 三十七、传输对象模式 ( Transfer Object Pattern )
- 三十八、设计模式资源