AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-design-patterns: What is front controller design pattern in java?

Please explain with example about front controller design pattern.

java x 211
design-patterns x 15
Posted On : 2014-05-16 18:25:31.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188236183
up-rate
4
down-rate

Answers


Front controller design pattern provides a centralized control to handle all the requests so that all the requests can be handled by a single handler. This handler can perform these tasks like authentication/ authorization/ tracking or logging of request and then pass the requests to corresponding handlers. Following are the components used in this pattern:

Front Controller- Single handler for all type of request coming to the application (web based/ desktop based).

Dispatcher- Front Controller may use a dispatcher object which is responsible to dispatch the request to corresponding specific handler.

View- Views are the object for which the requests are made.

Implementation:
----------------------------
We´re going to create a FrontController that is Dispatcher to act as Front Controller and Dispatcher correspondingly. HomeView and StudentView represent are views for which requests can come to front controller. Following is the flow diagram of FrontController design pattern.




Example of Front controller design pattern:
-------------------------------------------------------------

******************************** Step 1- Create Views ********************************

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");
}
}


******************************** Step 2- Create 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();
}
}
}


****************************** Step 3- Create 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){
//log each request
trackRequest(request);
//authenticate the user
if(isAuthenticUser()){
dispatcher.dispatch(request);
}
}
}


********* Step 4- Use the FrontController to demonstrate 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");
}
}


********************************* Step 5- Verify the output *********************************

Page requested: HOME
User is authenticated successfully.
Displaying Home Page
Page requested: STUDENT
User is authenticated successfully.
Displaying Student Page

Posted On : 2014-05-16 19:33:07
Satisfied : 2 Yes  1 No
profile Garima Gupta - anyforum.in Garima Gupta
596129543629
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in