Added Lazy Loading idiom.
This commit is contained in:
33
lazy-loading/src/main/java/com/iluwatar/App.java
Normal file
33
lazy-loading/src/main/java/com/iluwatar/App.java
Normal 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);
|
||||
}
|
||||
}
|
19
lazy-loading/src/main/java/com/iluwatar/Heavy.java
Normal file
19
lazy-loading/src/main/java/com/iluwatar/Heavy.java
Normal 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");
|
||||
}
|
||||
}
|
23
lazy-loading/src/main/java/com/iluwatar/HolderNaive.java
Normal file
23
lazy-loading/src/main/java/com/iluwatar/HolderNaive.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
34
lazy-loading/src/main/java/com/iluwatar/Java8Holder.java
Normal file
34
lazy-loading/src/main/java/com/iluwatar/Java8Holder.java
Normal 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();
|
||||
}
|
||||
}
|
12
lazy-loading/src/test/java/com/iluwatar/AppTest.java
Normal file
12
lazy-loading/src/test/java/com/iluwatar/AppTest.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user