30) front controller design pattern
类别:
问题:
方案:
示例:
public class FrontControllerPatternDemo { public static void main(String[] args) { FrontController frontController = new FrontController(); frontController.dispatchRequest("home"); frontController.dispatchRequest("student"); } } class HomeView { public void show() { System.out.println("Displaying Home Page"); } } class StudentView { public void show() { System.out.println("Displaying Student Page"); } } 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(); } } } 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); } } }
Page requested: home User is authenticated successfully. Displaying Home Page Page requested: student User is authenticated successfully. Displaying Student Page
应用:
不足:(
优化:)