#108 Consistent package naming throughout the examples

This commit is contained in:
Ilkka Seppala
2015-07-24 11:32:22 +03:00
parent af92d8dde5
commit 3d488ec15a
128 changed files with 963 additions and 873 deletions

View File

@ -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");
}
}

View File

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

View File

@ -0,0 +1,14 @@
package com.iluwatar.front.controller;
/**
*
* Command for archers.
*
*/
public class ArcherCommand implements Command {
@Override
public void process() {
new ArcherView().display();
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,14 @@
package com.iluwatar.front.controller;
/**
*
* Command for catapults.
*
*/
public class CatapultCommand implements Command {
@Override
public void process() {
new CatapultView().display();
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,11 @@
package com.iluwatar.front.controller;
/**
*
* Commands are the intermediary between requests and views.
*
*/
public interface Command {
void process();
}

View File

@ -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");
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,11 @@
package com.iluwatar.front.controller;
/**
*
* Views are the representations rendered for the user.
*
*/
public interface View {
void display();
}