I need a design pattern for storing in an object a reference to other objects that are created only when needed
A good analo开发者_如何学编程gy is a house. That house has rooms, and all of those rooms have people and objects. To begin with I only need to know the house exists with certain properties, but when I enter a room then I need to know the contents of those rooms. What is the best way to store a reference between the house and the rooms and the people and objects without creating a bunch of objects in memory?
Lazy loading.
public SomeObject getSomeObject() {
if (someObject == null) {
someObject = loadSomeObject(); // A private method.
}
return someObject;
}
This is implemented and supported widely in for example Hibernate/JPA.
精彩评论