|
2 | 2 |
|
3 | 3 | #### Q. Exaplain MVC, Front-Controller, DAO, DTO, Service-Locator, Prototype design patterns?
|
4 | 4 |
|
| 5 | +**2. Front-Controller** |
| 6 | + |
| 7 | +The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. Following are the entities of this type of design pattern. |
| 8 | + |
| 9 | +* Front Controller - Single handler for all kinds of requests coming to the application (either web based/ desktop based). |
| 10 | + |
| 11 | +* Dispatcher - Front Controller may use a dispatcher object which can dispatch the request to corresponding specific handler. |
| 12 | + |
| 13 | +* View - Views are the object for which the requests are made. |
| 14 | + |
| 15 | +Example: |
| 16 | + |
| 17 | +We are going to create a `FrontController` and `Dispatcher` to act as Front Controller and Dispatcher correspondingly. `HomeView` and `StudentView` represent various views for which requests can come to front controller. |
| 18 | + |
| 19 | +FrontControllerPatternDemo, our demo class, will use FrontController to demonstrate Front Controller Design Pattern. |
| 20 | + |
| 21 | +Step 1 |
| 22 | +Create Views. |
| 23 | + |
| 24 | +*HomeView.java* |
| 25 | +```java |
| 26 | +public class HomeView { |
| 27 | + public void show(){ |
| 28 | + System.out.println("Displaying Home Page"); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | +*StudentView.java* |
| 33 | +```java |
| 34 | +public class StudentView { |
| 35 | + public void show(){ |
| 36 | + System.out.println("Displaying Student Page"); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Step2 |
| 42 | +Create Dispatcher. |
| 43 | + |
| 44 | +*Dispatcher.java* |
| 45 | +```java |
| 46 | +public class Dispatcher { |
| 47 | + private StudentView studentView; |
| 48 | + private HomeView homeView; |
| 49 | + |
| 50 | + public Dispatcher(){ |
| 51 | + studentView = new StudentView(); |
| 52 | + homeView = new HomeView(); |
| 53 | + } |
| 54 | + |
| 55 | + public void dispatch(String request){ |
| 56 | + if(request.equalsIgnoreCase("STUDENT")){ |
| 57 | + studentView.show(); |
| 58 | + } |
| 59 | + else{ |
| 60 | + homeView.show(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +Step3 |
| 67 | +Create FrontController. |
| 68 | + |
| 69 | +*FrontController.java* |
| 70 | +```java |
| 71 | +public class FrontController { |
| 72 | + |
| 73 | + private Dispatcher dispatcher; |
| 74 | + |
| 75 | + public FrontController(){ |
| 76 | + dispatcher = new Dispatcher(); |
| 77 | + } |
| 78 | + |
| 79 | + private boolean isAuthenticUser(){ |
| 80 | + System.out.println("User is authenticated successfully."); |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + private void trackRequest(String request){ |
| 85 | + System.out.println("Page requested: " + request); |
| 86 | + } |
| 87 | + |
| 88 | + public void dispatchRequest(String request){ |
| 89 | + //log each request |
| 90 | + trackRequest(request); |
| 91 | + |
| 92 | + //authenticate the user |
| 93 | + if(isAuthenticUser()){ |
| 94 | + dispatcher.dispatch(request); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +Step4 |
| 101 | +Use the FrontController to demonstrate Front Controller Design Pattern. |
| 102 | + |
| 103 | +*FrontControllerPatternDemo.java* |
| 104 | +```java |
| 105 | +public class FrontControllerPatternDemo { |
| 106 | + public static void main(String[] args) { |
| 107 | + |
| 108 | + FrontController frontController = new FrontController(); |
| 109 | + frontController.dispatchRequest("HOME"); |
| 110 | + frontController.dispatchRequest("STUDENT"); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
5 | 115 | *ToDo*
|
6 | 116 |
|
7 | 117 | ## Q. What are the design patterns available in Java?
|
@@ -471,4 +581,4 @@ Output
|
471 | 581 |
|
472 | 582 | <div align="right">
|
473 | 583 | <b><a href="#">↥ back to top</a></b>
|
474 |
| -</div> |
| 584 | +</div> |
0 commit comments