X Tutup
Skip to content

Commit f42c340

Browse files
committed
updated flyweight sample
1 parent db4625b commit f42c340

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ public AlchemistShop() {
1515
}
1616

1717
private void fillShelves() {
18-
topShelf.add(new InvisibilityPotion());
19-
topShelf.add(new InvisibilityPotion());
20-
topShelf.add(new StrengthPotion());
21-
topShelf.add(new HealingPotion());
22-
topShelf.add(new InvisibilityPotion());
23-
topShelf.add(new StrengthPotion());
24-
topShelf.add(new HealingPotion());
25-
topShelf.add(new HealingPotion());
2618

27-
bottomShelf.add(new PoisonPotion());
28-
bottomShelf.add(new PoisonPotion());
29-
bottomShelf.add(new PoisonPotion());
30-
bottomShelf.add(new HolyWaterPotion());
31-
bottomShelf.add(new HolyWaterPotion());
19+
PotionFactory factory = new PotionFactory();
20+
21+
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
22+
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
23+
topShelf.add(factory.createPotion(PotionType.STRENGTH));
24+
topShelf.add(factory.createPotion(PotionType.HEALING));
25+
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
26+
topShelf.add(factory.createPotion(PotionType.STRENGTH));
27+
topShelf.add(factory.createPotion(PotionType.HEALING));
28+
topShelf.add(factory.createPotion(PotionType.HEALING));
29+
30+
bottomShelf.add(factory.createPotion(PotionType.POISON));
31+
bottomShelf.add(factory.createPotion(PotionType.POISON));
32+
bottomShelf.add(factory.createPotion(PotionType.POISON));
33+
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
34+
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
3235
}
3336

3437
public void enumerate() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class HealingPotion implements Potion {
44

55
@Override
66
public void drink() {
7-
System.out.println("You feel healed.");
7+
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
88
}
99

1010
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class HolyWaterPotion implements Potion {
44

55
@Override
66
public void drink() {
7-
System.out.println("You feel blessed.");
7+
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
88
}
99

1010
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class InvisibilityPotion implements Potion {
44

55
@Override
66
public void drink() {
7-
System.out.println("You become invisible.");
7+
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
88
}
99

1010
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class PoisonPotion implements Potion {
44

55
@Override
66
public void drink() {
7-
System.out.println("Urgh! This is poisonous.");
7+
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
88
}
99

1010
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@ Potion createPotion(PotionType type) {
1616
switch (type) {
1717
case HEALING:
1818
potion = new HealingPotion();
19+
potions.put(type, potion);
1920
break;
2021
case HOLY_WATER:
2122
potion = new HolyWaterPotion();
23+
potions.put(type, potion);
2224
break;
2325
case INVISIBILITY:
2426
potion = new InvisibilityPotion();
27+
potions.put(type, potion);
2528
break;
2629
case POISON:
2730
potion = new PoisonPotion();
31+
potions.put(type, potion);
2832
break;
2933
case STRENGTH:
3034
potion = new StrengthPotion();
35+
potions.put(type, potion);
3136
break;
3237
default:
3338
break;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class StrengthPotion implements Potion {
44

55
@Override
66
public void drink() {
7-
System.out.println("You feel strong.");
7+
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
88
}
99

1010
}

0 commit comments

Comments
 (0)
X Tutup