- 前控制器模式
- 设计模式-主页
- 设计模式-概述
- 设计模式-工厂模式
- 抽象工厂模式
- 设计模式-Singleton模式
- 设计模式-生成器模式
- 设计模式-原型模式
- 设计模式-适配器模式
- 设计模式-桥梁模式
- 设计模式-过滤器模式
- 设计图案-复合图案
- 设计图案-装饰图案
- 设计图案-立面图案
- 设计图案-飞线图案
- 设计模式-代理模式
- 责任链模式
- 设计模式-命令模式
- 设计模式-解释器模式
- 设计模式-迭代器模式
- 设计模式-中介模式
- 设计模式-Memento模式
- 设计模式-观察者模式
- 设计模式-状态模式
- 设计模式-空对象模式
- 设计模式-战略模式
- 设计模式-模板模式
- 设计模式-访客模式
- 设计模式-MVC模式
- 业务代表模式
- 复合实体模式
- 数据访问对象模式
- 前控制器模式
- 拦截过滤器模式
- 服务定位器模式
- Java传输对象模式
前控制器模式
The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. Following are the entities of this type of design pattern.
Front Controller - Single handler for all kinds of requests coming to the application (either web based/ desktop based).
Dispatcher - Front Controller may use a dispatcher object which can dispatch the request to corresponding specific handler.
View - Views are the object for which the requests are made.
Implementation
We are going to create a FrontController and Dispatcher to act as Front Controller and Dispatcher correspondingly. HomeView and StudentView represent various views for which requests can come to front controller.
FrontControllerPatternDemo, our demo class, will use FrontController to demonstrate Front Controller Design Pattern.
Step 1
Create Views.
HomeView.java
public class HomeView { public void show(){ System.out.println("Displaying Home Page"); } }
StudentView.java
public class StudentView { public void show(){ System.out.println("Displaying Student Page"); } }
Step 2
Create Dispatcher.
Dispatcher.java
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(); } } }
Step 3
Create FrontController
FrontController.java
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){ //log each request trackRequest(request); //authenticate the user if(isAuthenticUser()){ dispatcher.dispatch(request); } } }
Step 4
Use the FrontController to demonstrate Front Controller Design Pattern.
FrontControllerPatternDemo.java
public class FrontControllerPatternDemo { public static void main(String[] args) { FrontController frontController = new FrontController(); frontController.dispatchRequest("HOME"); frontController.dispatchRequest("STUDENT"); } }
Step 5
Verify the output.
Page requested: HOME User is authenticated successfully. Displaying Home Page Page requested: STUDENT User is authenticated successfully. Displaying Student Page