Skip to content

Commit 74b4330

Browse files
committed
Improved Flyweight example comments.
1 parent 57cef65 commit 74b4330

File tree

6 files changed

+19
-9
lines changed

6 files changed

+19
-9
lines changed

flyweight/src/main/java/com/iluwatar/AlchemistShop.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
/**
77
*
8-
* The class that needs many objects.
8+
* AlchemistShop holds potions on its shelves.
9+
* It uses PotionFactory to provide the potions.
910
*
1011
*/
1112
public class AlchemistShop {
@@ -52,6 +53,5 @@ public void enumerate() {
5253
for (Potion p : bottomShelf) {
5354
p.drink();
5455
}
55-
5656
}
5757
}

flyweight/src/main/java/com/iluwatar/App.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
/**
44
*
5-
* Flyweight (PotionFactory) is useful when there is plethora of objects
6-
* (Potion). It provides means to decrease resource usage by sharing object
7-
* instances.
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.
812
*
913
*/
1014
public class App {

flyweight/src/main/java/com/iluwatar/Potion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
*
5-
* Interface for objects.
5+
* Interface for Potions.
66
*
77
*/
88
public interface Potion {

flyweight/src/main/java/com/iluwatar/PotionFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
/**
66
*
7-
* Flyweight.
7+
* PotionFactory is the Flyweight in this example.
8+
* It minimizes memory use by sharing object instances.
9+
* It holds a map of potion instances and new potions
10+
* are created only when none of the type already exists.
811
*
912
*/
1013
public class PotionFactory {
@@ -45,5 +48,4 @@ Potion createPotion(PotionType type) {
4548
}
4649
return potion;
4750
}
48-
4951
}

flyweight/src/main/java/com/iluwatar/PotionType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Enumeration for potion types.
6+
*
7+
*/
38
public enum PotionType {
49

510
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON;

flyweight/src/main/java/com/iluwatar/StrengthPotion.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ public void drink() {
77
System.out.println("You feel strong. (Potion="
88
+ System.identityHashCode(this) + ")");
99
}
10-
1110
}

0 commit comments

Comments
 (0)