2015-07-24 11:32:22 +03:00
|
|
|
package com.iluwatar.lazy.loading;
|
2015-04-10 20:24:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Lazy loading idiom defers object creation until needed.
|
2015-08-19 22:09:54 +03:00
|
|
|
* <p>
|
2015-11-01 21:29:13 -05:00
|
|
|
* This example shows different implementations of the pattern with increasing sophistication.
|
2015-08-19 22:09:54 +03:00
|
|
|
* <p>
|
2015-04-10 20:24:16 +03:00
|
|
|
* Additional information and lazy loading flavours are described in
|
|
|
|
* http://martinfowler.com/eaaCatalog/lazyLoad.html
|
|
|
|
*
|
|
|
|
*/
|
2015-11-01 21:29:13 -05:00
|
|
|
public class App {
|
|
|
|
/**
|
|
|
|
* Program entry point
|
|
|
|
*
|
|
|
|
* @param args command line args
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
// Simple lazy loader - not thread safe
|
|
|
|
HolderNaive holderNaive = new HolderNaive();
|
|
|
|
Heavy heavy = holderNaive.getHeavy();
|
|
|
|
System.out.println("heavy=" + heavy);
|
|
|
|
|
|
|
|
// Thread safe lazy loader, but with heavy synchronization on each access
|
|
|
|
HolderThreadSafe holderThreadSafe = new HolderThreadSafe();
|
|
|
|
Heavy another = holderThreadSafe.getHeavy();
|
|
|
|
System.out.println("another=" + another);
|
|
|
|
|
|
|
|
// The most efficient lazy loader utilizing Java 8 features
|
|
|
|
Java8Holder java8Holder = new Java8Holder();
|
|
|
|
Heavy next = java8Holder.getHeavy();
|
|
|
|
System.out.println("next=" + next);
|
|
|
|
}
|
2015-04-10 20:24:16 +03:00
|
|
|
}
|