Package org.coliper.ibean.extension
Interface LazyInitChild
-
- All Known Subinterfaces:
LazyInit
public interface LazyInitChildExtension interface that supports with on-the-fly creation of nested IBeans. When IBean types reference each other you might want to chain getter calls to have short code and to avoid unnecessary temporary variables. The problem with chained getters is that you can easily run into null pointer scenarios. And here come the lazy initialization interfaces into play. They simply create the referenced IBean if it isnull.
See following example:
To have precise control over instance creation the logic is split into two interfaces:interface Address extends LazyInitChild { String getCity(); void setCity(String c); } interface Customer extends LazyInitParent { Address getAddress(); void setAddress(Address a); } void changeCity(String city) { Customer customer = ...; // Now here comes the code with the potential null pointer that is prevented by the // LazyInitXXX interfaces. If the customer does not yet own an Address object // a new Address instance is created on the fly. customer.getAddress().setCity(city); }LazyInitChildfor the bean type that might be created on demand andLazyInitParentfor the bean type that is referencing the child and whose getter calls are intercepted and checked for potential null returns.
LazyInitwhich is a combination ofLazyInitChildandLazyInitParent.LazyInitChildare always created with the same bean factory as theirLazyInitParents. That of course means that their bean style needs to match.