オブジェクト指向設計


コアJ2EEパターン - Front Controllerパターン

2008/06/29 20:28Update
TAGS: J2EE | パターン | Front Controller

Front Controller(フロント・コントローラー)パターンは コアJ2EEパターン の一つであり、WEBアプリケーションのフロント(Front)にコントローラー(Controller)を設置することで、WEBリクエストを集中して管理することができることになり、ビューのナビゲーション、認証、セッション管理などの共通処理に用いられています。

本文は「問題」、「解決策」、「戦略」、「メリット」、「実装例」などからFront Controllerパターンについて説明します。

問題


デザインパターンは設計上の問題を解決するために提出されています。

デザインが不適切な多くのWebアプリケーション(Webアプリケーションに限らず)では、クライアントは直接ビュー(JSPなど)にアクセスします。その結果、Webリクエストを集中管理ができなくなります。以下のような本来は共通であるべき処理でも、リクエストの制御が各ビューに分散しているため、同様なコードを各ビューに分散して重複記述しなければなりません。

◇ 認証
◇ ナビゲーション
◇ セッション管理
◇ ローカリゼーション
◇ ログ
◇ その他共通処理



こうしたシステムは、保守が困難になり、機能拡張もしにくくなります。例えば、セッションの内容の変化や認証方法の変更などがあった場合、すべてのビューソースを修正しなければなりません。

Front Controllerパターンは、これらの問題を解決します。

解決策


Front Controllerパターンを利用することによって、プレゼンテーション層とロジックを強制的に分離します。

<<図>>


Controllerはすべてのリクエストのエントリとなります。

戦略


Front Controller パターンに、いくつかの実装方法が存在します。

◇ Servlet(+ Dispatcher,+Command,+…)Front Controller戦略
◇ Filter Controller戦略
◇ JSP Front Controller戦略

一般的には、Servlet Front ControllerとFilter Controllerとの実装方法が薦められています。

メリット


◇ 集中して管理ができる。すべてのリクエストをFront Controllerに受け取っているため、認証、セッション管理などの共通処理を一元管理することができます。
◇ 保守性と拡張性の向上。

実装例


Servlet Controllerの実装サンプルです。

public class SampleController extends HttpServlet {
  protected void processRequest(HttpServletRequest
    request, HttpServletResponse response)
    throws ServletException, java.io.IOException {

    String page = null;
    try {
      //process authority
      processAuthority(request, response);
      //manage session
      processSession(request, response);

      // Use a helper object to gather parameter
      // specific information.
      RequestHelper helper = new
         RequestHelper(request);

      Command cmdHelper= helper.getCommand();

      // Command helper perform custom operation
      page = cmdHelper.execute(request, response);

    }
    catch (Exception e) {
      LogManager.logMessage("Error Message HERE");
      page = resource.getErrorPage(e);
    }
    // dispatch control to view
    dispatch(request, response, page);
  }

  protected void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, java.io.IOException {
      processRequest(request, response);
  }

  protected void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
  }

  protected boolean processAuthority(HttpServletRequest request,
    HttpServletResponse response) {
      ... //authority control code here
  }

  protected void processSession(HttpServletRequest request,
    HttpServletResponse response) {
      ... //session management code here
  }

  protected void dispatch(HttpServletRequest request,
    HttpServletResponse response,
    String page)
  throws  javax.servlet.ServletException,
    java.io.IOException {
    RequestDispatcher dispatcher =
      getServletContext().getRequestDispatcher(page);
    dispatcher.forward(request, response);
  }
}


その他参考資料


Core J2EE Pattern Catalog 
Core J2EE Patterns - Front Controller 

有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Sponsored Link


Comments