#90 Added comments for the example code.
This commit is contained in:
parent
c4556561c4
commit
a9fa304690
@ -1,5 +1,23 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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 class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Command for archers.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class ArcherCommand implements Command {
|
public class ArcherCommand implements Command {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* View for archers.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class ArcherView implements View {
|
public class ArcherView implements View {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Command for catapults.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class CatapultCommand implements Command {
|
public class CatapultCommand implements Command {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* View for catapults.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class CatapultView implements View {
|
public class CatapultView implements View {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Commands are the intermediary between requests and views.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public interface Command {
|
public interface Command {
|
||||||
|
|
||||||
void process();
|
void process();
|
||||||
|
14
front-controller/src/main/java/com/iluwatar/ErrorView.java
Normal file
14
front-controller/src/main/java/com/iluwatar/ErrorView.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* View for errors.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ErrorView implements View {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void display() {
|
||||||
|
System.out.println("Error 500");
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,11 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* FrontController is the handler class that takes in all the requests and
|
||||||
|
* renders the correct response.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class FrontController {
|
public class FrontController {
|
||||||
|
|
||||||
public void handleRequest(String request) {
|
public void handleRequest(String request) {
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Default command in case the mapping is not successful.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class UnknownCommand implements Command {
|
public class UnknownCommand implements Command {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process() {
|
public void process() {
|
||||||
System.out.println("Error 500");
|
new ErrorView().display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Views are the representations rendered for the user.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public interface View {
|
public interface View {
|
||||||
|
|
||||||
void display();
|
void display();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user