Skip to content

Commit 525c930

Browse files
committed
BAEL-305: Refactored Book and Bookshelf as interfaces and implemented them.
1 parent ffe5ce6 commit 525c930

6 files changed

Lines changed: 87 additions & 60 deletions

File tree

‎enterprise-patterns/front-controller-pattern/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<dependency>
1818
<groupId>javax.servlet</groupId>
1919
<artifactId>javax.servlet-api</artifactId>
20-
<version>3.1.0</version>
20+
<version>4.0.0-b01</version>
2121
<scope>provided</scope>
2222
</dependency>
2323
</dependencies>

‎enterprise-patterns/front-controller-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package com.baeldung.enterprise.patterns.front.controller.commands;
22

33
import com.baeldung.enterprise.patterns.front.controller.data.Book;
4-
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
4+
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
55

66
import javax.servlet.ServletException;
77
import java.io.IOException;
88

99
public class SearchCommand extends FrontCommand {
1010
@Override
1111
public void process() throws ServletException, IOException {
12-
Book book = Bookshelf.getInstance()
12+
Book book = new BookshelfImpl().getInstance()
1313
.findByTitle(request.getParameter("title"));
1414
if (book != null) {
1515
request.setAttribute("book", book);
1616
forward("book-found");
1717
} else {
18-
request.setAttribute("books", Bookshelf.getInstance().getBooks());
1918
forward("book-notfound");
2019
}
2120
}
Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
11
package com.baeldung.enterprise.patterns.front.controller.data;
22

3-
public class Book {
4-
private String author;
5-
private String title;
6-
private Double price;
3+
public interface Book {
4+
String getAuthor();
75

8-
public Book() {
9-
}
6+
void setAuthor(String author);
107

11-
public Book(String author, String title, Double price) {
12-
this.author = author;
13-
this.title = title;
14-
this.price = price;
15-
}
8+
String getTitle();
169

17-
public String getAuthor() {
18-
return author;
19-
}
10+
void setTitle(String title);
2011

21-
public void setAuthor(String author) {
22-
this.author = author;
23-
}
12+
Double getPrice();
2413

25-
public String getTitle() {
26-
return title;
27-
}
28-
29-
public void setTitle(String title) {
30-
this.title = title;
31-
}
32-
33-
public Double getPrice() {
34-
return price;
35-
}
36-
37-
public void setPrice(Double price) {
38-
this.price = price;
39-
}
14+
void setPrice(Double price);
4015
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
public class BookImpl implements Book {
4+
private String author;
5+
private String title;
6+
private Double price;
7+
8+
public BookImpl() {
9+
}
10+
11+
public BookImpl(String author, String title, Double price) {
12+
this.author = author;
13+
this.title = title;
14+
this.price = price;
15+
}
16+
17+
@Override
18+
public String getAuthor() {
19+
return author;
20+
}
21+
22+
@Override
23+
public void setAuthor(String author) {
24+
this.author = author;
25+
}
26+
27+
@Override
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
@Override
33+
public void setTitle(String title) {
34+
this.title = title;
35+
}
36+
37+
@Override
38+
public Double getPrice() {
39+
return price;
40+
}
41+
42+
@Override
43+
public void setPrice(Double price) {
44+
this.price = price;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
package com.baeldung.enterprise.patterns.front.controller.data;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
public interface Bookshelf {
54

6-
public class Bookshelf {
7-
private static Bookshelf INSTANCE = new Bookshelf();
8-
private List<Book> books = new ArrayList<>();
9-
10-
public static Bookshelf getInstance() {
11-
if (INSTANCE.books.size() == 0) {
12-
INSTANCE.init();
13-
}
14-
return INSTANCE;
5+
default void init() {
6+
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
7+
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
158
}
169

17-
private void init() {
18-
books.add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
19-
books.add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
20-
}
10+
Bookshelf getInstance();
2111

22-
public Book findByTitle(String title) {
23-
return books.stream()
24-
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
25-
.findFirst()
26-
.orElse(null);
27-
}
12+
<E extends Book> boolean add(E book);
2813

29-
public List<Book> getBooks() {
30-
return books;
31-
}
14+
Book findByTitle(String title);
3215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
import java.util.ArrayList;
4+
5+
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
6+
private static Bookshelf INSTANCE;
7+
8+
@Override
9+
public Bookshelf getInstance() {
10+
if (INSTANCE == null) {
11+
INSTANCE = new BookshelfImpl();
12+
INSTANCE.init();
13+
}
14+
return INSTANCE;
15+
}
16+
17+
@Override
18+
public Book findByTitle(String title) {
19+
return this.stream()
20+
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
21+
.findFirst()
22+
.orElse(null);
23+
}
24+
}

0 commit comments

Comments
 (0)