Merge pull request #657 from christian-raedel/craedel-enterprise-patterns/front-controller
BAEL-305: enterprise patterns/front controller
This commit is contained in:
@@ -31,7 +31,11 @@
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<<<<<<< HEAD
|
||||
<version>9.3.11.v20160721</version>
|
||||
=======
|
||||
<version>9.4.0.M1</version>
|
||||
>>>>>>> upstream/master
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/front-controller</contextPath>
|
||||
|
||||
+6
-13
@@ -22,24 +22,17 @@ public class FrontControllerServlet extends HttpServlet {
|
||||
|
||||
private FrontCommand getCommand(HttpServletRequest request) {
|
||||
try {
|
||||
return (FrontCommand) getCommandClass(request)
|
||||
.asSubclass(FrontCommand.class)
|
||||
.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to get command!", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Class getCommandClass(HttpServletRequest request) {
|
||||
try {
|
||||
return Class.forName(
|
||||
Class type = Class.forName(
|
||||
String.format(
|
||||
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
|
||||
request.getParameter("command")
|
||||
)
|
||||
);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return UnknownCommand.class;
|
||||
return (FrontCommand) type
|
||||
.asSubclass(FrontCommand.class)
|
||||
.newInstance();
|
||||
} catch (Exception e) {
|
||||
return new UnknownCommand();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-32
@@ -1,40 +1,15 @@
|
||||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public class Book {
|
||||
private String author;
|
||||
private String title;
|
||||
private Double price;
|
||||
public interface Book {
|
||||
String getAuthor();
|
||||
|
||||
public Book() {
|
||||
}
|
||||
void setAuthor(String author);
|
||||
|
||||
public Book(String author, String title, Double price) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
String getTitle();
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
void setTitle(String title);
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
Double getPrice();
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
void setPrice(Double price);
|
||||
}
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public class BookImpl implements Book {
|
||||
private String author;
|
||||
private String title;
|
||||
private Double price;
|
||||
|
||||
public BookImpl() {
|
||||
}
|
||||
|
||||
public BookImpl(String author, String title, Double price) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -3,8 +3,8 @@ package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
public interface Bookshelf {
|
||||
|
||||
default void init() {
|
||||
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
}
|
||||
|
||||
Bookshelf getInstance();
|
||||
|
||||
Reference in New Issue
Block a user