#108 Consistent package naming throughout the examples
This commit is contained in:
@ -0,0 +1,29 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Front Controller is a presentation tier pattern. Essentially it defines a
|
||||
* controller that handles all requests for a web site.
|
||||
*
|
||||
* The Front Controller pattern consolidates request handling through a single handler
|
||||
* object (FrontController). This object can carry out the common the behavior such as
|
||||
* authorization, request logging and routing requests to corresponding views.
|
||||
*
|
||||
* Typically the requests are mapped to command objects (Command) which then display
|
||||
* the correct view (View).
|
||||
*
|
||||
* In this example we have implemented two views: ArcherView and CatapultView. These
|
||||
* are displayed by sending correct request to the FrontController object. For example,
|
||||
* the ArcherView gets displayed when FrontController receives request "Archer". When
|
||||
* the request is unknown, we display the error view (ErrorView).
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
FrontController controller = new FrontController();
|
||||
controller.handleRequest("Archer");
|
||||
controller.handleRequest("Catapult");
|
||||
controller.handleRequest("foobar");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
public class ApplicationException extends RuntimeException {
|
||||
|
||||
public ApplicationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Command for archers.
|
||||
*
|
||||
*/
|
||||
public class ArcherCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
new ArcherView().display();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* View for archers.
|
||||
*
|
||||
*/
|
||||
public class ArcherView implements View {
|
||||
|
||||
@Override
|
||||
public void display() {
|
||||
System.out.println("Displaying archers");
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Command for catapults.
|
||||
*
|
||||
*/
|
||||
public class CatapultCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
new CatapultView().display();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* View for catapults.
|
||||
*
|
||||
*/
|
||||
public class CatapultView implements View {
|
||||
|
||||
@Override
|
||||
public void display() {
|
||||
System.out.println("Displaying catapults");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Commands are the intermediary between requests and views.
|
||||
*
|
||||
*/
|
||||
public interface Command {
|
||||
|
||||
void process();
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* View for errors.
|
||||
*
|
||||
*/
|
||||
public class ErrorView implements View {
|
||||
|
||||
@Override
|
||||
public void display() {
|
||||
System.out.println("Error 500");
|
||||
}
|
||||
}
|
@ -0,0 +1,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;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Default command in case the mapping is not successful.
|
||||
*
|
||||
*/
|
||||
public class UnknownCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
new ErrorView().display();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Views are the representations rendered for the user.
|
||||
*
|
||||
*/
|
||||
public interface View {
|
||||
|
||||
void display();
|
||||
}
|
Reference in New Issue
Block a user