#90 Finished the example code
This commit is contained in:
		| @@ -1,7 +1,11 @@ | |||||||
| package com.iluwatar; | package com.iluwatar; | ||||||
|  |  | ||||||
| public class App { | public class App { | ||||||
|  | 	 | ||||||
| 	public static void main(String[] args) { | 	public static void main(String[] args) { | ||||||
| 		System.out.println("Hello World!"); | 		FrontController controller = new FrontController(); | ||||||
|  | 		controller.handleRequest("Archer"); | ||||||
|  | 		controller.handleRequest("Catapult"); | ||||||
|  | 		controller.handleRequest("foobar"); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,8 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class ApplicationException extends RuntimeException { | ||||||
|  |  | ||||||
|  |     public ApplicationException(Throwable cause) { | ||||||
|  |         super(cause); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class ArcherCommand implements Command { | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public void process() { | ||||||
|  | 		new ArcherView().display(); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class ArcherView implements View { | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public void display() { | ||||||
|  | 		System.out.println("Displaying archers"); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class CatapultCommand implements Command { | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public void process() { | ||||||
|  | 		new CatapultView().display(); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class CatapultView implements View { | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public void display() { | ||||||
|  | 		System.out.println("Displaying catapults"); | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										6
									
								
								front-controller/src/main/java/com/iluwatar/Command.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								front-controller/src/main/java/com/iluwatar/Command.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public interface Command { | ||||||
|  | 	 | ||||||
|  | 	void process(); | ||||||
|  | } | ||||||
| @@ -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; | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class UnknownCommand implements Command { | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public void process() { | ||||||
|  | 		System.out.println("Error 500"); | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										6
									
								
								front-controller/src/main/java/com/iluwatar/View.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								front-controller/src/main/java/com/iluwatar/View.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public interface View { | ||||||
|  |  | ||||||
|  | 	void display(); | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user