forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrontController.java
More file actions
34 lines (30 loc) · 770 Bytes
/
FrontController.java
File metadata and controls
34 lines (30 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.iluwatar.front.controller;
/**
*
* FrontController is the handler class that takes in all the requests and
* renders the correct response.
*
*/
public class FrontController {
public void handleRequest(String request) {
Command command = getCommand(request);
command.process();
}
private Command getCommand(String request) {
Class commandClass = getCommandClass(request);
try {
return (Command) commandClass.newInstance();
} catch (Exception e) {
throw new ApplicationException(e);
}
}
private Class getCommandClass(String request) {
Class result;
try {
result = Class.forName("com.iluwatar." + request + "Command");
} catch (ClassNotFoundException e) {
result = UnknownCommand.class;
}
return result;
}
}