これはLaw of Demeter
に違反していますか?以下の${book.author.name}
および${book.category.name}
を参照してください。式言語でJSPでこれを行うのはいいですか?私はここでデメテルの法則に違反していますか?
<c:set var="book" value="${book}" />
<table>
<tr>
<td>Title:</td><td><c:out value="${book.title}" /></td>
</tr>
<tr>
<td><c:out value="${book.description}" /></td>
</tr>
<tr>
<td>Price: </td><td><c:out value="${book.price}" /></td>
</tr>
<tr>
<td>Author: </td><td><c:out value="${book.author.name}" /></td>
</tr>
<tr>
<td>Category: </td><td><c:out value="${book.category.name}" /></td>
</tr>
</table>
</body>
</html>
${book}
属性はBook
オブジェクトです。 BookServlet
:BookService -> BookDao
、厳密な意味では
public Book findBookById(int id) throws DaoException {
Book book = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = this.getConnection();
ps = con.prepareStatement(FIND_BOOK_BY_ID);
ps.setInt(1, id);
//Using a PreparedStatement to execute SQL...
rs = ps.executeQuery();
while (rs.next()) {
int bookId = rs.getInt("book_id");
String title = rs.getString("title");
String desc = rs.getString("description");
int authorId = rs.getInt("author_id");
String authorName = rs.getString("author_name");
int categoryId = rs.getInt("category_id");
String categoryName = rs.getString("book_category_name");
double price = rs.getDouble("price");
book = new Book(bookId);
book.setTitle(title);
book.setDescription(desc);
book.setPrice(price);
Author author = new Author(authorId);
author.setName(authorName);
book.setAuthor(author);
Category category = new Category(categoryId);
category.setName(categoryName);
book.setCategory(category);
}
} catch (SQLException e) {
throw new DaoException("findBookById() " + e.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
freeConnection(con);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage());
}
}
return book;
}
おそらく、これはcodereview.stackexchange.comでより良いでしょう。 –
私はちょうど 'デメテルの法律を誤解 '@ http://www.dan-manges.com/blog/37を読んでいます。彼は '景色'でやってもいいと言いますか? – Jonathan
もうひとつ取り上げます - [Demeterの法則はドットカウントの練習ではありません](http://haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspx)Philハック – McDowell