HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Front Controller pattern (Front Controller Pattern) is used to provide a centralized mechanism for handling requests, all requests are processed by a single handler. The handler can do authentication / authorization / logging or tracking request, then the request to the appropriate handler. The following is an entity of this design pattern.
We will createFrontController,Dispatcher, respectively, as the front-end controller and scheduler.HomeViewandStudentViewview showing a variety of front-end controller receives a request to create.
FrontControllerPatternDemo,our demonstration class to demonstrate the useFrontControllerFront Controller design pattern.
Create the view.
HomeView.java
public class HomeView { public void show () { System.out.println ( "Displaying Home Page"); } }
StudentView.java
public class StudentView { public void show () { System.out.println ( "Displaying Student Page"); } }
Create dispatcher Dispatcher.
Dispatcher.java
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 (); } } }
Creating a front controller FrontController.
Context.java
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) { // Record every request trackRequest (request); // To authenticate the user if (isAuthenticUser ()) { dispatcher.dispatch (request); } } }
FrontControllerto demonstrate the use of the Front Controller design pattern.
FrontControllerPatternDemo.java
public class FrontControllerPatternDemo { public static void main (String [] args) { FrontController frontController = new FrontController (); frontController.dispatchRequest ( "HOME"); frontController.dispatchRequest ( "STUDENT"); } }
Verify output.
Page requested: HOME User is authenticated successfully. Displaying Home Page Page requested: STUDENT User is authenticated successfully. Displaying Student Page