BAEL-305: Refactored Book and Bookshelf as interfaces and implemented them.

This commit is contained in:
Christian Rädel 2016-09-01 21:41:37 +02:00
parent ffe5ce630c
commit 525c93059c
6 changed files with 87 additions and 60 deletions

View File

@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version> <version>4.0.0-b01</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,7 +1,7 @@
package com.baeldung.enterprise.patterns.front.controller.commands; package com.baeldung.enterprise.patterns.front.controller.commands;
import com.baeldung.enterprise.patterns.front.controller.data.Book; import com.baeldung.enterprise.patterns.front.controller.data.Book;
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf; import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import java.io.IOException; import java.io.IOException;
@ -9,13 +9,12 @@ import java.io.IOException;
public class SearchCommand extends FrontCommand { public class SearchCommand extends FrontCommand {
@Override @Override
public void process() throws ServletException, IOException { public void process() throws ServletException, IOException {
Book book = Bookshelf.getInstance() Book book = new BookshelfImpl().getInstance()
.findByTitle(request.getParameter("title")); .findByTitle(request.getParameter("title"));
if (book != null) { if (book != null) {
request.setAttribute("book", book); request.setAttribute("book", book);
forward("book-found"); forward("book-found");
} else { } else {
request.setAttribute("books", Bookshelf.getInstance().getBooks());
forward("book-notfound"); forward("book-notfound");
} }
} }

View File

@ -1,40 +1,15 @@
package com.baeldung.enterprise.patterns.front.controller.data; package com.baeldung.enterprise.patterns.front.controller.data;
public class Book { public interface Book {
private String author; String getAuthor();
private String title;
private Double price;
public Book() { void setAuthor(String author);
}
public Book(String author, String title, Double price) { String getTitle();
this.author = author;
this.title = title;
this.price = price;
}
public String getAuthor() { void setTitle(String title);
return author;
}
public void setAuthor(String author) { Double getPrice();
this.author = author;
}
public String getTitle() { void setPrice(Double price);
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
} }

View File

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

View File

@ -1,32 +1,15 @@
package com.baeldung.enterprise.patterns.front.controller.data; package com.baeldung.enterprise.patterns.front.controller.data;
import java.util.ArrayList; public interface Bookshelf {
import java.util.List;
public class Bookshelf { default void init() {
private static Bookshelf INSTANCE = new Bookshelf(); add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
private List<Book> books = new ArrayList<>(); add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
public static Bookshelf getInstance() {
if (INSTANCE.books.size() == 0) {
INSTANCE.init();
}
return INSTANCE;
} }
private void init() { Bookshelf getInstance();
books.add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
books.add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
}
public Book findByTitle(String title) { <E extends Book> boolean add(E book);
return books.stream()
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
.findFirst()
.orElse(null);
}
public List<Book> getBooks() { Book findByTitle(String title);
return books;
}
} }

View File

@ -0,0 +1,24 @@
package com.baeldung.enterprise.patterns.front.controller.data;
import java.util.ArrayList;
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
private static Bookshelf INSTANCE;
@Override
public Bookshelf getInstance() {
if (INSTANCE == null) {
INSTANCE = new BookshelfImpl();
INSTANCE.init();
}
return INSTANCE;
}
@Override
public Book findByTitle(String title) {
return this.stream()
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
.findFirst()
.orElse(null);
}
}