Interface LazyInitChild

  • All Known Subinterfaces:
    LazyInit

    public interface LazyInitChild
    Extension 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 is null.

    See following example:

     
     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);
     } 
     
     
    To have precise control over instance creation the logic is split into two interfaces:
    • LazyInitChild for the bean type that might be created on demand and
    • LazyInitParent for the bean type that is referencing the child and whose getter calls are intercepted and checked for potential null returns.
    Often you might not want to distinguish between parents and children. In that case you can simply extend all your bean types with LazyInit which is a combination of LazyInitChild and LazyInitParent.

    LazyInitChild are always created with the same bean factory as their LazyInitParents. That of course means that their bean style needs to match.