diff --git a/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java b/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java index bfeb27e6a..e87f2e6cf 100644 --- a/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java +++ b/flyweight/src/main/java/com/iluwatar/flyweight/AlchemistShop.java @@ -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 topShelf; - List 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 topShelf; + private List 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(); + } + } +} diff --git a/flyweight/src/main/java/com/iluwatar/flyweight/App.java b/flyweight/src/main/java/com/iluwatar/flyweight/App.java index f88dd5346..3aa41cf2c 100644 --- a/flyweight/src/main/java/com/iluwatar/flyweight/App.java +++ b/flyweight/src/main/java/com/iluwatar/flyweight/App.java @@ -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(); + } +}