私のアプリケーションは、要求に応じてデータベースから情報を取得し、その情報からオブジェクトを生成します。私は現在、このタスクを完了するために 2 つの異なる手法を検討しています (ただし、他の手法にもオープンです!)。
方法 1:
class Book {
private int id;
private String author;
private String title;
public Book(int id) {
ResultSet book = getBookFromDatabaseById(id);
this.id = book.id;
this.author = book.author;
// ...
}
}
方法 2:
public class Book {
private HashMap<String, Object> propertyContainer;
public Book(int id) {
this.propertyContainer = getBookFromDatabaseById(id);
}
public Object getProperty(String propertyKey) {
return this.propertyContainer.get(propertyKey);
}
}
方法 1 では、プロパティの制御、制限、および場合によってはアクセスが容易であると思いますが、新しいプロパティの追加は方法 2 の方がスムーズになります。
これを行う適切な方法は何ですか?