|
1 | | -package com.iluwatar.flyweight; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | - |
6 | | -/** |
7 | | - * |
8 | | - * AlchemistShop holds potions on its shelves. |
9 | | - * It uses PotionFactory to provide the potions. |
10 | | - * |
11 | | - */ |
12 | | -public class AlchemistShop { |
13 | | - |
14 | | - List<Potion> topShelf; |
15 | | - List<Potion> bottomShelf; |
16 | | - |
17 | | - public AlchemistShop() { |
18 | | - topShelf = new ArrayList<>(); |
19 | | - bottomShelf = new ArrayList<>(); |
20 | | - fillShelves(); |
21 | | - } |
22 | | - |
23 | | - private void fillShelves() { |
24 | | - |
25 | | - PotionFactory factory = new PotionFactory(); |
26 | | - |
27 | | - topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); |
28 | | - topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); |
29 | | - topShelf.add(factory.createPotion(PotionType.STRENGTH)); |
30 | | - topShelf.add(factory.createPotion(PotionType.HEALING)); |
31 | | - topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); |
32 | | - topShelf.add(factory.createPotion(PotionType.STRENGTH)); |
33 | | - topShelf.add(factory.createPotion(PotionType.HEALING)); |
34 | | - topShelf.add(factory.createPotion(PotionType.HEALING)); |
35 | | - |
36 | | - bottomShelf.add(factory.createPotion(PotionType.POISON)); |
37 | | - bottomShelf.add(factory.createPotion(PotionType.POISON)); |
38 | | - bottomShelf.add(factory.createPotion(PotionType.POISON)); |
39 | | - bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER)); |
40 | | - bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER)); |
41 | | - } |
42 | | - |
43 | | - public void enumerate() { |
44 | | - |
45 | | - System.out.println("Enumerating top shelf potions\n"); |
46 | | - |
47 | | - for (Potion p : topShelf) { |
48 | | - p.drink(); |
49 | | - } |
50 | | - |
51 | | - System.out.println("\nEnumerating bottom shelf potions\n"); |
52 | | - |
53 | | - for (Potion p : bottomShelf) { |
54 | | - p.drink(); |
55 | | - } |
56 | | - } |
57 | | -} |
| 1 | +package com.iluwatar.flyweight; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * |
| 8 | + * AlchemistShop holds potions on its shelves. |
| 9 | + * It uses PotionFactory to provide the potions. |
| 10 | + * |
| 11 | + */ |
| 12 | +public class AlchemistShop { |
| 13 | + |
| 14 | + private List<Potion> topShelf; |
| 15 | + private List<Potion> bottomShelf; |
| 16 | + |
| 17 | + public AlchemistShop() { |
| 18 | + topShelf = new ArrayList<>(); |
| 19 | + bottomShelf = new ArrayList<>(); |
| 20 | + fillShelves(); |
| 21 | + } |
| 22 | + |
| 23 | + private void fillShelves() { |
| 24 | + |
| 25 | + PotionFactory factory = new PotionFactory(); |
| 26 | + |
| 27 | + topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); |
| 28 | + topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); |
| 29 | + topShelf.add(factory.createPotion(PotionType.STRENGTH)); |
| 30 | + topShelf.add(factory.createPotion(PotionType.HEALING)); |
| 31 | + topShelf.add(factory.createPotion(PotionType.INVISIBILITY)); |
| 32 | + topShelf.add(factory.createPotion(PotionType.STRENGTH)); |
| 33 | + topShelf.add(factory.createPotion(PotionType.HEALING)); |
| 34 | + topShelf.add(factory.createPotion(PotionType.HEALING)); |
| 35 | + |
| 36 | + bottomShelf.add(factory.createPotion(PotionType.POISON)); |
| 37 | + bottomShelf.add(factory.createPotion(PotionType.POISON)); |
| 38 | + bottomShelf.add(factory.createPotion(PotionType.POISON)); |
| 39 | + bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER)); |
| 40 | + bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER)); |
| 41 | + } |
| 42 | + |
| 43 | + public void enumerate() { |
| 44 | + |
| 45 | + System.out.println("Enumerating top shelf potions\n"); |
| 46 | + |
| 47 | + for (Potion p : topShelf) { |
| 48 | + p.drink(); |
| 49 | + } |
| 50 | + |
| 51 | + System.out.println("\nEnumerating bottom shelf potions\n"); |
| 52 | + |
| 53 | + for (Potion p : bottomShelf) { |
| 54 | + p.drink(); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments