File tree Expand file tree Collapse file tree 7 files changed +107
-73
lines changed
main/java/com/iluwatar/flyweight
test/java/com/iluwatar/flyweight Expand file tree Collapse file tree 7 files changed +107
-73
lines changed Original file line number Diff line number Diff line change 44 *
55 * Flyweight pattern is useful when the program needs a huge amount of objects.
66 * 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
7+ * <p>
8+ * In this example {@link AlchemistShop} has great amount of potions on its shelves.
9+ * To fill the shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents
10+ * the Flyweight in this example). Internally {@link PotionFactory} holds a map
1111 * of the potions and lazily creates new ones when requested.
12- *
12+ * <p>
1313 * To enable safe sharing, between clients and threads, Flyweight objects must
1414 * be immutable. Flyweight objects are by definition value objects.
1515 *
1616 */
1717public class App {
1818
19+ /**
20+ * Program entry point
21+ * @param args command line args
22+ */
1923 public static void main (String [] args ) {
2024 AlchemistShop alchemistShop = new AlchemistShop ();
2125 alchemistShop .enumerate ();
Original file line number Diff line number Diff line change 1- package com .iluwatar .flyweight ;
2-
3- public class HealingPotion implements Potion {
4-
5- @ Override
6- public void drink () {
7- System .out .println ("You feel healed. (Potion="
8- + System .identityHashCode (this ) + ")" );
9- }
10-
11- }
1+ package com .iluwatar .flyweight ;
2+
3+ /**
4+ *
5+ * HealingPotion
6+ *
7+ */
8+ public class HealingPotion implements Potion {
9+
10+ @ Override
11+ public void drink () {
12+ System .out .println ("You feel healed. (Potion="
13+ + System .identityHashCode (this ) + ")" );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .flyweight ;
2-
3- public class HolyWaterPotion implements Potion {
4-
5- @ Override
6- public void drink () {
7- System .out .println ("You feel blessed. (Potion="
8- + System .identityHashCode (this ) + ")" );
9- }
10-
11- }
1+ package com .iluwatar .flyweight ;
2+
3+ /**
4+ *
5+ * HolyWaterPotion
6+ *
7+ */
8+ public class HolyWaterPotion implements Potion {
9+
10+ @ Override
11+ public void drink () {
12+ System .out .println ("You feel blessed. (Potion="
13+ + System .identityHashCode (this ) + ")" );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .flyweight ;
2-
3- public class InvisibilityPotion implements Potion {
4-
5- @ Override
6- public void drink () {
7- System .out .println ("You become invisible. (Potion="
8- + System .identityHashCode (this ) + ")" );
9- }
10-
11- }
1+ package com .iluwatar .flyweight ;
2+
3+ /**
4+ *
5+ * InvisibilityPotion
6+ *
7+ */
8+ public class InvisibilityPotion implements Potion {
9+
10+ @ Override
11+ public void drink () {
12+ System .out .println ("You become invisible. (Potion="
13+ + System .identityHashCode (this ) + ")" );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .flyweight ;
2-
3- public class PoisonPotion implements Potion {
4-
5- @ Override
6- public void drink () {
7- System .out .println ("Urgh! This is poisonous. (Potion="
8- + System .identityHashCode (this ) + ")" );
9- }
10-
11- }
1+ package com .iluwatar .flyweight ;
2+
3+ /**
4+ *
5+ * PoisonPotion
6+ *
7+ */
8+ public class PoisonPotion implements Potion {
9+
10+ @ Override
11+ public void drink () {
12+ System .out .println ("Urgh! This is poisonous. (Potion="
13+ + System .identityHashCode (this ) + ")" );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .flyweight ;
2-
3- public class StrengthPotion implements Potion {
4-
5- @ Override
6- public void drink () {
7- System .out .println ("You feel strong. (Potion="
8- + System .identityHashCode (this ) + ")" );
9- }
10- }
1+ package com .iluwatar .flyweight ;
2+
3+ /**
4+ *
5+ * StrengthPotion
6+ *
7+ */
8+ public class StrengthPotion implements Potion {
9+
10+ @ Override
11+ public void drink () {
12+ System .out .println ("You feel strong. (Potion="
13+ + System .identityHashCode (this ) + ")" );
14+ }
15+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .flyweight ;
2-
3- import org .junit .Test ;
4-
5- import com .iluwatar .flyweight .App ;
6-
7- public class AppTest {
8-
9- @ Test
10- public void test () {
11- String [] args = {};
12- App .main (args );
13- }
14- }
1+ package com .iluwatar .flyweight ;
2+
3+ import org .junit .Test ;
4+
5+ import com .iluwatar .flyweight .App ;
6+
7+ /**
8+ *
9+ * Application test
10+ *
11+ */
12+ public class AppTest {
13+
14+ @ Test
15+ public void test () {
16+ String [] args = {};
17+ App .main (args );
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments