#108 Consistent package naming throughout the examples

This commit is contained in:
Ilkka Seppala
2015-07-24 11:32:22 +03:00
parent af92d8dde5
commit 3d488ec15a
128 changed files with 963 additions and 873 deletions

View File

@ -0,0 +1,33 @@
package com.iluwatar.lazy.loading;
/**
*
* 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.lazy.loading;
/**
*
* 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.lazy.loading;
/**
*
* 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.lazy.loading;
/**
*
* 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.lazy.loading;
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();
}
}