专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

三十四、前端控制器模式(Front Controller Pattern)

文章永久连接:https://tech.souyunku.com/2719

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

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

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

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

实现

img_1.png

1、 定义类 FrontControllerDispatcher 分别当作前端控制器和调度器
2、 定义类 HomeViewStudentView 表示作为前端控制器接收到的请求而创建的视图
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

干货推荐

本站推荐:精选优质专栏

附录:设计模式:系列文章

未经允许不得转载:搜云库技术团队 » 三十四、前端控制器模式(Front Controller Pattern)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们