[Design Pattern] Front Controller Pattern Simple case

Source: Internet
Author: User

The Front controller pattern, the front-end controller mode, is used to centralize user requests so that all requests are processed by the same front-end controller, processing the content for authentication, authorization, logging, and tracing requests, and then handing over the request to the processor to the Distributor.

The front-end controller mode mainly involves the following three characters

Front-end controllers (Front Controller)-one processor to handle all user requests

Dispatcher (Dispatcher)-distributes processed requests to the business handler for

View-Real Process Request Business Program

The following is a simple case of a front-end controller.

Homeview, Studentview is the specific business processing procedure respectively. The Dispatcher is used to distribute the request to the View. Frontcontroller is the portal for all user requests, for authentication, permission validation, logging, or tracing request logs. The Frontcontrollerdemo demonstrates the front-end controller mode.

Code implementation

Homeview, Studentview provides true business processing logic

 Public class Homeview {    publicvoid  Show () {        System.out.println ("show Home View") ;    }}
 Public class Studentview {    publicvoid  Show () {        System.out.println ("Show Student View ");}    }

Dispatcher distributing the user's request to the corresponding business handler

 Public classDispatcher {PrivateStudentview Studentview; PrivateHomeview Homeview;  PublicDispatcher () {Homeview=NewHomeview (); Studentview=NewStudentview (); }         Public voidDispatch (String viewName) {if("Homeview". Equals (ViewName))        {homeview.show (); }        Else{studentview.show (); }    }    }

Frontcontroller is used to process all user requests, authenticate, verify permissions, request records, or trace, and then submit the request to Dispatcher

 Public classFrontcontroller {PrivateDispatcher Dispatcher;  PublicFrontcontroller () {Dispatcher=NewDispatcher (); }         Public BooleanIsauthenticuser () {System.out.println ("Authenticate User"); return true; }         Public voidtrackrequest (String viewName) {System.out.println ("Track Request" +viewName); }         Public voiddispatchrequest (String viewName) {trackrequest (viewName); if(Isauthenticuser ()) {Dispatcher.dispatch (viewName); }    }}

Demonstrates front-end controller mode.

 Public class Frontcontrollerpatterndemo {    publicstaticvoid  main () {                  New  Frontcontroller ();                 = "Homeview";        Frontcontroller.dispatchrequest (viewName);                 = "Studentview";        Frontcontroller.dispatchrequest (ViewName);            }}

Resources

Design pattern-front Controller Pattern, Tutorialspoint

[Design Pattern] Front Controller Pattern Simple case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.