Skip to content

Commit

Permalink
BAEL-305: Refactored Book and Bookshelf as interfaces and implemented…
Browse files Browse the repository at this point in the history
… them.
  • Loading branch information
incredible-io committed Sep 1, 2016
1 parent ffe5ce6 commit 525c930
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 60 deletions.
2 changes: 1 addition & 1 deletion enterprise-patterns/front-controller-pattern/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<version>4.0.0-b01</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
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.Bookshelf;
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;

import javax.servlet.ServletException;
import java.io.IOException;

public class SearchCommand extends FrontCommand {
@Override
public void process() throws ServletException, IOException {
Book book = Bookshelf.getInstance()
Book book = new BookshelfImpl().getInstance()
.findByTitle(request.getParameter("title"));
if (book != null) {
request.setAttribute("book", book);
forward("book-found");
} else {
request.setAttribute("books", Bookshelf.getInstance().getBooks());
forward("book-notfound");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
package com.baeldung.enterprise.patterns.front.controller.data;

import java.util.ArrayList;
import java.util.List;
public interface Bookshelf {

public class Bookshelf {
private static Bookshelf INSTANCE = new Bookshelf();
private List<Book> books = new ArrayList<>();

public static Bookshelf getInstance() {
if (INSTANCE.books.size() == 0) {
INSTANCE.init();
}
return INSTANCE;
default void init() {
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
}

private void init() {
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));
}
Bookshelf getInstance();

public Book findByTitle(String title) {
return books.stream()
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
.findFirst()
.orElse(null);
}
<E extends Book> boolean add(E book);

public List<Book> getBooks() {
return books;
}
Book findByTitle(String title);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 525c930

Please sign in to comment.