Skip to content

Commit c455656

Browse files
committed
iluwatar#90 Finished the example code
1 parent 4ad5e84 commit c455656

10 files changed

Lines changed: 98 additions & 1 deletion

File tree

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.iluwatar;
22

33
public class App {
4+
45
public static void main(String[] args) {
5-
System.out.println("Hello World!");
6+
FrontController controller = new FrontController();
7+
controller.handleRequest("Archer");
8+
controller.handleRequest("Catapult");
9+
controller.handleRequest("foobar");
610
}
711
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar;
2+
3+
public class ApplicationException extends RuntimeException {
4+
5+
public ApplicationException(Throwable cause) {
6+
super(cause);
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public class ArcherCommand implements Command {
4+
5+
@Override
6+
public void process() {
7+
new ArcherView().display();
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public class ArcherView implements View {
4+
5+
@Override
6+
public void display() {
7+
System.out.println("Displaying archers");
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public class CatapultCommand implements Command {
4+
5+
@Override
6+
public void process() {
7+
new CatapultView().display();
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public class CatapultView implements View {
4+
5+
@Override
6+
public void display() {
7+
System.out.println("Displaying catapults");
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.iluwatar;
2+
3+
public interface Command {
4+
5+
void process();
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar;
2+
3+
public class FrontController {
4+
5+
public void handleRequest(String request) {
6+
Command command = getCommand(request);
7+
command.process();
8+
}
9+
10+
private Command getCommand(String request) {
11+
Class commandClass = getCommandClass(request);
12+
try {
13+
return (Command) commandClass.newInstance();
14+
} catch (Exception e) {
15+
throw new ApplicationException(e);
16+
}
17+
}
18+
19+
private Class getCommandClass(String request) {
20+
Class result;
21+
try {
22+
result = Class.forName("com.iluwatar." + request + "Command");
23+
} catch (ClassNotFoundException e) {
24+
result = UnknownCommand.class;
25+
}
26+
return result;
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public class UnknownCommand implements Command {
4+
5+
@Override
6+
public void process() {
7+
System.out.println("Error 500");
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.iluwatar;
2+
3+
public interface View {
4+
5+
void display();
6+
}

0 commit comments

Comments
 (0)