Reformat rest of the design patterns - Issue #224

This commit is contained in:
Ankur Kaushal
2015-11-01 21:29:13 -05:00
parent 449340bd2b
commit 306b1f3d31
337 changed files with 6744 additions and 6851 deletions

View File

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

View File

@ -7,13 +7,13 @@ package com.iluwatar.lazy.loading;
*/
public class Heavy {
public Heavy() {
System.out.println("Creating Heavy ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("... Heavy created");
}
public Heavy() {
System.out.println("Creating Heavy ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("... Heavy created");
}
}

View File

@ -2,22 +2,21 @@ package com.iluwatar.lazy.loading;
/**
*
* Simple implementation of the lazy loading idiom.
* However, this is not thread safe.
* 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;
}
private Heavy heavy;
public HolderNaive() {
System.out.println("HolderNaive created");
}
public Heavy getHeavy() {
if (heavy == null) {
heavy = new Heavy();
}
return heavy;
}
}

View File

@ -2,23 +2,22 @@ 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.
* 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;
}
private Heavy heavy;
public HolderThreadSafe() {
System.out.println("HolderThreadSafe created");
}
public synchronized Heavy getHeavy() {
if (heavy == null) {
heavy = new Heavy();
}
return heavy;
}
}

View File

@ -4,31 +4,34 @@ import java.util.function.Supplier;
/**
*
* This lazy loader is thread safe and more efficient than {@link HolderThreadSafe}.
* It utilizes Java 8 functional interface {@link Supplier<T>} as {@link Heavy} factory.
* This lazy loader is thread safe and more efficient than {@link HolderThreadSafe}. It utilizes
* Java 8 functional interface {@link Supplier<T>} as {@link 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();
}
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

@ -11,9 +11,9 @@ import com.iluwatar.lazy.loading.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -8,35 +8,38 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* Using reflection this test shows that the heavy field is not instantiated until the method getHeavy is called
* Using reflection this test shows that the heavy field is not instantiated until the method
* getHeavy is called
*
* Created by jones on 11/10/2015.
*/
public class HolderThreadSafeTest {
@Test
public void test() throws IllegalAccessException {
HolderThreadSafe hts = new HolderThreadSafe();
@Test
public void test() throws IllegalAccessException {
HolderThreadSafe hts = new HolderThreadSafe();
{//first call is null
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
for (Field f: ff) {
f.setAccessible(true);
}
{
// first call is null
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
for (Field f : ff) {
f.setAccessible(true);
}
assertNull(ff[0].get(hts));
}
// now it is lazily loaded
hts.getHeavy();
{//now it is not null - call via reflection so that the test is the same before and after
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
for (Field f: ff) {
f.setAccessible(true);
}
assertNotNull(ff[0].get(hts));
}
assertNull(ff[0].get(hts));
}
// now it is lazily loaded
hts.getHeavy();
{
// now it is not null - call via reflection so that the test is the same before and after
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
for (Field f : ff) {
f.setAccessible(true);
}
assertNotNull(ff[0].get(hts));
}
}
}