Skip to content

Latest commit

 

History

History
45 lines (24 loc) · 2.42 KB

File metadata and controls

45 lines (24 loc) · 2.42 KB

Lazy Load

Scenario

Multiglom B2B, vendor of the CRM suite Manjok for small businesses, got a couple of customer complaints about performance problems in the finance module. Investigations have shown that the issue is related to invoices, especially if there are many. Due to the fact how invoices are stored it takes too much time to load them, even if the user is only interested in the overview and not in the details.

The goal is improving the invoice list view performance without affecting the application functionality.

Choice of Pattern

In this scenario we want to apply the Lazy Load Pattern to provide an object that doesn't contain all of the data you need but knows how to get it (Fowler).

Test

Initially, the supplier only partially fills the LazyLoadObject with data.

Test

When the client later tries to access not yet loaded information, a retrieval process gets triggered. The latter happens transparent to the client.

In the given scenario the Invoice is the object which takes long to load it entirely. Thus we implement Invoice as a LazyLoad object.

Test

The PersistenceSession first returns a LazyLoad object to the client.

Test

In the moment when the client tries to access not yet loaded invoice details (call getDebtorName() for example), the information will be loaded from the database.

This way the invoice list view provided by Manjok can be loaded quickly. Loading the details of a particular invoice will be deferred or omitted at all if the user is not interested in the invoice details.

Try it out!

Open LazyLoadTest.java to start playing with this pattern. By setting the log-level for this pattern to DEBUG in logback.xml you can watch the pattern working step by step.

Remarks

  • As discussed in the book a lazy loading can be tricky. Thus, besides the Lazy Load functionality, the code-example demonstrates some common issues: the ripple-effect, closed session and lost session.

References

  • (Fowler) Fowler, M.: Patterns of Enterprise Application Architecture. Addison-Wesley (2002)