You can override the getter for PhotoAlbum
in Profile
to create an album on demand:
class Profile {
...
PhotoAlbum getPhotoAlbum() {
if (photoAlbum == null) {
photoAlbum = new PhotoAlbum()
}
photoAlbum
}
}
Then when you call profile.photoAlbum
, it will be created automatically as you expected. This will create empty albums whenever the getter is called, though, which might not be what you want. I'd make it more explicit, like this:
class Profile {
...
PhotoAlbum createOrGetPhotoAlbum() {
if (photoAlbum == null) {
photoAlbum = new PhotoAlbum()
}
photoAlbum
}
}
And call it like this: profile.createOrGetPhotoAlbum().addToPhotos(photo)