Added Lazy Loading idiom.

This commit is contained in:
Ilkka Seppala
2015-04-10 20:24:16 +03:00
parent 484e1ddfca
commit 33566805ee
11 changed files with 246 additions and 2 deletions

View File

@ -0,0 +1,33 @@
package com.iluwatar;
/**
*
* Lazy loading idiom defers object creation until needed.
*
* This example shows different implementations of the pattern
* with increasing sophistication.
*
* Additional information and lazy loading flavours are described in
* http://martinfowler.com/eaaCatalog/lazyLoad.html
*
*/
public class App
{
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);
}
}

View File

@ -0,0 +1,19 @@
package com.iluwatar;
/**
*
* Heavy objects are expensive to create.
*
*/
public class Heavy {
public Heavy() {
System.out.println("Creating Heavy ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("... Heavy created");
}
}

View File

@ -0,0 +1,23 @@
package com.iluwatar;
/**
*
* Simple implementation of the lazy loading idiom.
* However, this is not thread safe.
*
*/
public class HolderNaive {
private Heavy heavy;
public HolderNaive() {
System.out.println("HolderNaive created");
}
public Heavy getHeavy() {
if (heavy == null) {
heavy = new Heavy();
}
return heavy;
}
}

View File

@ -0,0 +1,24 @@
package com.iluwatar;
/**
*
* Same as HolderNaive but with added synchronization.
* This implementation is thread safe, but each {@link #getHeavy()}
* call costs additional synchronization overhead.
*
*/
public class HolderThreadSafe {
private Heavy heavy;
public HolderThreadSafe() {
System.out.println("HolderThreadSafe created");
}
public synchronized Heavy getHeavy() {
if (heavy == null) {
heavy = new Heavy();
}
return heavy;
}
}

View File

@ -0,0 +1,34 @@
package com.iluwatar;
import java.util.function.Supplier;
/**
*
* This lazy loader is thread safe and more efficient than HolderThreadSafe.
* It utilizes Java 8 functional interface Supplier<T> as Heavy factory.
*
*/
public class Java8Holder {
private Supplier<Heavy> heavy = () -> createAndCacheHeavy();
public Java8Holder() {
System.out.println("Java8Holder created");
}
public Heavy getHeavy() {
return heavy.get();
}
private synchronized Heavy createAndCacheHeavy() {
class HeavyFactory implements Supplier<Heavy> {
private final Heavy heavyInstance = new Heavy();
@Override
public Heavy get() { return heavyInstance; }
}
if (!HeavyFactory.class.isInstance(heavy)) {
heavy = new HeavyFactory();
}
return heavy.get();
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar;
import org.junit.Test;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}