Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion java-design-pattern-questions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,116 @@

#### Q. Exaplain MVC, Front-Controller, DAO, DTO, Service-Locator, Prototype design patterns?

**2. Front-Controller**

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.

* Front Controller - Single handler for all kinds of requests coming to the application (either web based/ desktop based).

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

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

Example:

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.

FrontControllerPatternDemo, our demo class, will use FrontController to demonstrate Front Controller Design Pattern.

Step 1
Create Views.

*HomeView.java*
```java
public class HomeView {
public void show(){
System.out.println("Displaying Home Page");
}
}
```
*StudentView.java*
```java
public class StudentView {
public void show(){
System.out.println("Displaying Student Page");
}
}
```

Step2
Create Dispatcher.

*Dispatcher.java*
```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();
}
}
}
```

Step3
Create FrontController.

*FrontController.java*
```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);
}
}
}
```

Step4
Use the FrontController to demonstrate Front Controller Design Pattern.

*FrontControllerPatternDemo.java*
```java
public class FrontControllerPatternDemo {
public static void main(String[] args) {

FrontController frontController = new FrontController();
frontController.dispatchRequest("HOME");
frontController.dispatchRequest("STUDENT");
}
}
```

*ToDo*

## Q. What are the design patterns available in Java?
Expand Down Expand Up @@ -471,4 +581,4 @@ Output

<div align="right">
<b><a href="#">↥ back to top</a></b>
</div>
</div>