Skip to content

Commit b952469

Browse files
committed
Added comments about object sharing in Flyweight example + some minor
fixes.
1 parent 2a9de6a commit b952469

File tree

2 files changed

+80
-77
lines changed

2 files changed

+80
-77
lines changed
Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,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-
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+
}
Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
package com.iluwatar.flyweight;
2-
3-
/**
4-
*
5-
* Flyweight pattern is useful when the program needs a huge amount of objects.
6-
* It provides means to decrease resource usage by sharing object instances.
7-
*
8-
* In this example AlchemistShop has great amount of potions on its shelves.
9-
* To fill the shelves AlchemistShop uses PotionFactory (which represents
10-
* the Flyweight in this example). Internally PotionFactory holds a map
11-
* of the potions and lazily creates new ones when requested.
12-
*
13-
*/
14-
public class App {
15-
16-
public static void main(String[] args) {
17-
AlchemistShop alchemistShop = new AlchemistShop();
18-
alchemistShop.enumerate();
19-
}
20-
}
1+
package com.iluwatar.flyweight;
2+
3+
/**
4+
*
5+
* Flyweight pattern is useful when the program needs a huge amount of objects.
6+
* It provides means to decrease resource usage by sharing object instances.
7+
*
8+
* In this example AlchemistShop has great amount of potions on its shelves.
9+
* To fill the shelves AlchemistShop uses PotionFactory (which represents
10+
* the Flyweight in this example). Internally PotionFactory holds a map
11+
* of the potions and lazily creates new ones when requested.
12+
*
13+
* To enable safe sharing, between clients and threads, Flyweight objects must
14+
* be immutable. Flyweight objects are by definition value objects.
15+
*
16+
*/
17+
public class App {
18+
19+
public static void main(String[] args) {
20+
AlchemistShop alchemistShop = new AlchemistShop();
21+
alchemistShop.enumerate();
22+
}
23+
}

0 commit comments

Comments
 (0)