我正在閱讀Martin Fowlers的一本書,即企業應用程序模式:ftp://ftp.heanet.ie/mirrors/sourceforge/w/we/webtune/Patterns%20of%20Enterprise%20Application%20Architecture.pdf。我已經涉及了行數據網關模式,特別是註冊表模式。下面是這本書的第149頁的代碼片段:使用行數據網關的註冊表模式
public static Person find(Long id) {
Person result = (Person) Registry.getPerson(id);
if (result != null)
return result;
PreparedStatement findStatement = null;
ResultSet rs = null;
try {
findStatement = DB.prepare(findStatementString);
findStatement.setLong(1, id.longValue());
rs = findStatement.executeQuery();
rs.next();
result = load(rs);
return result;
} catch (SQLException e) {
throw new ApplicationException(e);
} finally {
DB.cleanUp(findStatement, rs);
}
}
上面的代碼調用登記類,但我不知道註冊表類的好處是什麼。我已閱讀關於註冊表課程的章節,但我仍不清楚。我明白他們是靜態方法。
爲什麼你需要通過Book - > Author來獲取City對象?你能提供一個註冊表的例子嗎?感謝+1。 – w0051977
@ w0051977你不會這樣做,但是在一個更復雜的例子中,你可能不得不爲了在對象層次結構中找到一些實例。 –
謝謝。因此,不要在所有地方傳遞id,而是將它傳遞到註冊表並將該對象保存在註冊表中。 – w0051977