mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 18:59:10 +00:00
added flyweight sample
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AlchemistShop {
|
||||
|
||||
List<Potion> topShelf;
|
||||
List<Potion> bottomShelf;
|
||||
|
||||
public AlchemistShop() {
|
||||
topShelf = new ArrayList<>();
|
||||
bottomShelf = new ArrayList<>();
|
||||
fillShelves();
|
||||
}
|
||||
|
||||
private void fillShelves() {
|
||||
topShelf.add(new InvisibilityPotion());
|
||||
topShelf.add(new InvisibilityPotion());
|
||||
topShelf.add(new StrengthPotion());
|
||||
topShelf.add(new HealingPotion());
|
||||
topShelf.add(new InvisibilityPotion());
|
||||
topShelf.add(new StrengthPotion());
|
||||
topShelf.add(new HealingPotion());
|
||||
topShelf.add(new HealingPotion());
|
||||
|
||||
bottomShelf.add(new PoisonPotion());
|
||||
bottomShelf.add(new PoisonPotion());
|
||||
bottomShelf.add(new PoisonPotion());
|
||||
bottomShelf.add(new HolyWaterPotion());
|
||||
bottomShelf.add(new HolyWaterPotion());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user