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