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