#107 Flyweight example JavaDoc

This commit is contained in:
Ilkka Seppala 2015-08-18 22:51:08 +03:00
parent 25a2fc756d
commit de784cfdc1
7 changed files with 107 additions and 73 deletions

View File

@ -4,18 +4,22 @@ package com.iluwatar.flyweight;
* *
* Flyweight pattern is useful when the program needs a huge amount of objects. * Flyweight pattern is useful when the program needs a huge amount of objects.
* It provides means to decrease resource usage by sharing object instances. * It provides means to decrease resource usage by sharing object instances.
* * <p>
* In this example AlchemistShop has great amount of potions on its shelves. * In this example {@link AlchemistShop} has great amount of potions on its shelves.
* To fill the shelves AlchemistShop uses PotionFactory (which represents * To fill the shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents
* the Flyweight in this example). Internally PotionFactory holds a map * the Flyweight in this example). Internally {@link PotionFactory} holds a map
* of the potions and lazily creates new ones when requested. * of the potions and lazily creates new ones when requested.
* * <p>
* To enable safe sharing, between clients and threads, Flyweight objects must * To enable safe sharing, between clients and threads, Flyweight objects must
* be immutable. Flyweight objects are by definition value objects. * be immutable. Flyweight objects are by definition value objects.
* *
*/ */
public class App { public class App {
/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) { public static void main(String[] args) {
AlchemistShop alchemistShop = new AlchemistShop(); AlchemistShop alchemistShop = new AlchemistShop();
alchemistShop.enumerate(); alchemistShop.enumerate();

View File

@ -1,11 +1,16 @@
package com.iluwatar.flyweight; package com.iluwatar.flyweight;
public class HealingPotion implements Potion { /**
*
@Override * HealingPotion
public void drink() { *
System.out.println("You feel healed. (Potion=" */
+ System.identityHashCode(this) + ")"); public class HealingPotion implements Potion {
}
@Override
} public void drink() {
System.out.println("You feel healed. (Potion="
+ System.identityHashCode(this) + ")");
}
}

View File

@ -1,11 +1,16 @@
package com.iluwatar.flyweight; package com.iluwatar.flyweight;
public class HolyWaterPotion implements Potion { /**
*
@Override * HolyWaterPotion
public void drink() { *
System.out.println("You feel blessed. (Potion=" */
+ System.identityHashCode(this) + ")"); public class HolyWaterPotion implements Potion {
}
@Override
} public void drink() {
System.out.println("You feel blessed. (Potion="
+ System.identityHashCode(this) + ")");
}
}

View File

@ -1,11 +1,16 @@
package com.iluwatar.flyweight; package com.iluwatar.flyweight;
public class InvisibilityPotion implements Potion { /**
*
@Override * InvisibilityPotion
public void drink() { *
System.out.println("You become invisible. (Potion=" */
+ System.identityHashCode(this) + ")"); public class InvisibilityPotion implements Potion {
}
@Override
} public void drink() {
System.out.println("You become invisible. (Potion="
+ System.identityHashCode(this) + ")");
}
}

View File

@ -1,11 +1,16 @@
package com.iluwatar.flyweight; package com.iluwatar.flyweight;
public class PoisonPotion implements Potion { /**
*
@Override * PoisonPotion
public void drink() { *
System.out.println("Urgh! This is poisonous. (Potion=" */
+ System.identityHashCode(this) + ")"); public class PoisonPotion implements Potion {
}
@Override
} public void drink() {
System.out.println("Urgh! This is poisonous. (Potion="
+ System.identityHashCode(this) + ")");
}
}

View File

@ -1,10 +1,15 @@
package com.iluwatar.flyweight; package com.iluwatar.flyweight;
public class StrengthPotion implements Potion { /**
*
@Override * StrengthPotion
public void drink() { *
System.out.println("You feel strong. (Potion=" */
+ System.identityHashCode(this) + ")"); public class StrengthPotion implements Potion {
}
} @Override
public void drink() {
System.out.println("You feel strong. (Potion="
+ System.identityHashCode(this) + ")");
}
}

View File

@ -1,14 +1,19 @@
package com.iluwatar.flyweight; package com.iluwatar.flyweight;
import org.junit.Test; import org.junit.Test;
import com.iluwatar.flyweight.App; import com.iluwatar.flyweight.App;
public class AppTest { /**
*
@Test * Application test
public void test() { *
String[] args = {}; */
App.main(args); public class AppTest {
}
} @Test
public void test() {
String[] args = {};
App.main(args);
}
}