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

View File

@ -1,11 +1,16 @@
package com.iluwatar.flyweight;
public class HealingPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel healed. (Potion="
+ System.identityHashCode(this) + ")");
}
}
package com.iluwatar.flyweight;
/**
*
* HealingPotion
*
*/
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;
public class HolyWaterPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel blessed. (Potion="
+ System.identityHashCode(this) + ")");
}
}
package com.iluwatar.flyweight;
/**
*
* HolyWaterPotion
*
*/
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;
public class InvisibilityPotion implements Potion {
@Override
public void drink() {
System.out.println("You become invisible. (Potion="
+ System.identityHashCode(this) + ")");
}
}
package com.iluwatar.flyweight;
/**
*
* InvisibilityPotion
*
*/
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;
public class PoisonPotion implements Potion {
@Override
public void drink() {
System.out.println("Urgh! This is poisonous. (Potion="
+ System.identityHashCode(this) + ")");
}
}
package com.iluwatar.flyweight;
/**
*
* PoisonPotion
*
*/
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;
public class StrengthPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel strong. (Potion="
+ System.identityHashCode(this) + ")");
}
}
package com.iluwatar.flyweight;
/**
*
* StrengthPotion
*
*/
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;
import org.junit.Test;
import com.iluwatar.flyweight.App;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}
package com.iluwatar.flyweight;
import org.junit.Test;
import com.iluwatar.flyweight.App;
/**
*
* Application test
*
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}