コアJ2EEパターン - Front Controllerパターン
2008/06/29 20:28Update
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の実装サンプルです。
Core J2EE Pattern Catalog 
Core J2EE Patterns - 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
Sponsored Link
- Relative Articles
- ASP.NETでのフロント・コントローラ(Front Controller)実装 - (2009/10/20 15:22)
- Factory Method パターン - ファクトリメソッドパターン - (2008/06/09 22:44)
- Abstract Factory パターン - 抽象ファクトリパターン - (2008/06/10 13:25)
- Singleton パターン - シングルトンパターン - (2008/06/10 15:36)
- Prototypeパターン - プロトタイプパターン - (2008/06/10 17:23)
- Builderパターン - ビルダーパターン - (2008/06/10 19:01)
- Adapter パターン - アダプターパターン - (2008/06/11 11:30)
- Composite パターン - コンポジットパターン - (2008/06/11 16:59)
- Interpreter パターン - インタプリタパターン - (2008/06/13 15:42)
- Chain of Responsibility パターン - 責任の連鎖パターン - (2008/06/14 00:28)