Added comments about object sharing in Flyweight example + some minor
fixes.
This commit is contained in:
@ -1,57 +1,57 @@
|
|||||||
package com.iluwatar.flyweight;
|
package com.iluwatar.flyweight;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* AlchemistShop holds potions on its shelves.
|
* AlchemistShop holds potions on its shelves.
|
||||||
* It uses PotionFactory to provide the potions.
|
* It uses PotionFactory to provide the potions.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AlchemistShop {
|
public class AlchemistShop {
|
||||||
|
|
||||||
List<Potion> topShelf;
|
private List<Potion> topShelf;
|
||||||
List<Potion> bottomShelf;
|
private List<Potion> bottomShelf;
|
||||||
|
|
||||||
public AlchemistShop() {
|
public AlchemistShop() {
|
||||||
topShelf = new ArrayList<>();
|
topShelf = new ArrayList<>();
|
||||||
bottomShelf = new ArrayList<>();
|
bottomShelf = new ArrayList<>();
|
||||||
fillShelves();
|
fillShelves();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fillShelves() {
|
private void fillShelves() {
|
||||||
|
|
||||||
PotionFactory factory = new PotionFactory();
|
PotionFactory factory = new PotionFactory();
|
||||||
|
|
||||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||||
topShelf.add(factory.createPotion(PotionType.HEALING));
|
topShelf.add(factory.createPotion(PotionType.HEALING));
|
||||||
|
|
||||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
bottomShelf.add(factory.createPotion(PotionType.POISON));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||||
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enumerate() {
|
public void enumerate() {
|
||||||
|
|
||||||
System.out.println("Enumerating top shelf potions\n");
|
System.out.println("Enumerating top shelf potions\n");
|
||||||
|
|
||||||
for (Potion p : topShelf) {
|
for (Potion p : topShelf) {
|
||||||
p.drink();
|
p.drink();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("\nEnumerating bottom shelf potions\n");
|
System.out.println("\nEnumerating bottom shelf potions\n");
|
||||||
|
|
||||||
for (Potion p : bottomShelf) {
|
for (Potion p : bottomShelf) {
|
||||||
p.drink();
|
p.drink();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,23 @@
|
|||||||
package com.iluwatar.flyweight;
|
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.
|
||||||
*
|
*
|
||||||
* In this example AlchemistShop has great amount of potions on its shelves.
|
* In this example AlchemistShop has great amount of potions on its shelves.
|
||||||
* To fill the shelves AlchemistShop uses PotionFactory (which represents
|
* To fill the shelves AlchemistShop uses PotionFactory (which represents
|
||||||
* the Flyweight in this example). Internally PotionFactory holds a map
|
* the Flyweight in this example). Internally PotionFactory holds a map
|
||||||
* of the potions and lazily creates new ones when requested.
|
* of the potions and lazily creates new ones when requested.
|
||||||
*
|
*
|
||||||
*/
|
* To enable safe sharing, between clients and threads, Flyweight objects must
|
||||||
public class App {
|
* be immutable. Flyweight objects are by definition value objects.
|
||||||
|
*
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
AlchemistShop alchemistShop = new AlchemistShop();
|
public class App {
|
||||||
alchemistShop.enumerate();
|
|
||||||
}
|
public static void main(String[] args) {
|
||||||
}
|
AlchemistShop alchemistShop = new AlchemistShop();
|
||||||
|
alchemistShop.enumerate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user