#90 Finished the example code

This commit is contained in:
Ilkka Seppala 2015-07-19 12:44:51 +03:00
parent 4ad5e84d0e
commit c4556561c4
10 changed files with 98 additions and 1 deletions

View File

@ -1,7 +1,11 @@
package com.iluwatar;
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
FrontController controller = new FrontController();
controller.handleRequest("Archer");
controller.handleRequest("Catapult");
controller.handleRequest("foobar");
}
}

View File

@ -0,0 +1,8 @@
package com.iluwatar;
public class ApplicationException extends RuntimeException {
public ApplicationException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class ArcherCommand implements Command {
@Override
public void process() {
new ArcherView().display();
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class ArcherView implements View {
@Override
public void display() {
System.out.println("Displaying archers");
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class CatapultCommand implements Command {
@Override
public void process() {
new CatapultView().display();
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class CatapultView implements View {
@Override
public void display() {
System.out.println("Displaying catapults");
}
}

View File

@ -0,0 +1,6 @@
package com.iluwatar;
public interface Command {
void process();
}

View File

@ -0,0 +1,28 @@
package com.iluwatar;
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;
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class UnknownCommand implements Command {
@Override
public void process() {
System.out.println("Error 500");
}
}

View File

@ -0,0 +1,6 @@
package com.iluwatar;
public interface View {
void display();
}