File tree Expand file tree Collapse file tree 10 files changed +74
-1
lines changed
front-controller/src/main/java/com/iluwatar Expand file tree Collapse file tree 10 files changed +74
-1
lines changed Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * The Front Controller is a presentation tier pattern. Essentially it defines a
6+ * controller that handles all requests for a web site.
7+ *
8+ * The Front Controller pattern consolidates request handling through a single handler
9+ * object (FrontController). This object can carry out the common the behavior such as
10+ * authorization, request logging and routing requests to corresponding views.
11+ *
12+ * Typically the requests are mapped to command objects (Command) which then display
13+ * the correct view (View).
14+ *
15+ * In this example we have implemented two views: ArcherView and CatapultView. These
16+ * are displayed by sending correct request to the FrontController object. For example,
17+ * the ArcherView gets displayed when FrontController receives request "Archer". When
18+ * the request is unknown, we display the error view (ErrorView).
19+ *
20+ */
321public class App {
422
523 public static void main (String [] args ) {
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Command for archers.
6+ *
7+ */
38public class ArcherCommand implements Command {
49
510 @ Override
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * View for archers.
6+ *
7+ */
38public class ArcherView implements View {
49
510 @ Override
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Command for catapults.
6+ *
7+ */
38public class CatapultCommand implements Command {
49
510 @ Override
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * View for catapults.
6+ *
7+ */
38public class CatapultView implements View {
49
510 @ Override
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Commands are the intermediary between requests and views.
6+ *
7+ */
38public interface Command {
49
510 void process ();
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ /**
4+ *
5+ * View for errors.
6+ *
7+ */
8+ public class ErrorView implements View {
9+
10+ @ Override
11+ public void display () {
12+ System .out .println ("Error 500" );
13+ }
14+ }
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * FrontController is the handler class that takes in all the requests and
6+ * renders the correct response.
7+ *
8+ */
39public class FrontController {
410
511 public void handleRequest (String request ) {
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Default command in case the mapping is not successful.
6+ *
7+ */
38public class UnknownCommand implements Command {
49
510 @ Override
611 public void process () {
7- System . out . println ( "Error 500" );
12+ new ErrorView (). display ( );
813 }
914}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Views are the representations rendered for the user.
6+ *
7+ */
38public interface View {
49
510 void display ();
You can’t perform that action at this time.
0 commit comments