updated flyweight sample

This commit is contained in:
Ilkka Seppala 2014-08-23 19:13:09 +03:00
parent db4625b9fe
commit f42c340598
7 changed files with 26 additions and 18 deletions

View File

@ -15,20 +15,23 @@ public class AlchemistShop {
}
private void fillShelves() {
topShelf.add(new InvisibilityPotion());
topShelf.add(new InvisibilityPotion());
topShelf.add(new StrengthPotion());
topShelf.add(new HealingPotion());
topShelf.add(new InvisibilityPotion());
topShelf.add(new StrengthPotion());
topShelf.add(new HealingPotion());
topShelf.add(new HealingPotion());
bottomShelf.add(new PoisonPotion());
bottomShelf.add(new PoisonPotion());
bottomShelf.add(new PoisonPotion());
bottomShelf.add(new HolyWaterPotion());
bottomShelf.add(new HolyWaterPotion());
PotionFactory factory = new PotionFactory();
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
topShelf.add(factory.createPotion(PotionType.STRENGTH));
topShelf.add(factory.createPotion(PotionType.HEALING));
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
topShelf.add(factory.createPotion(PotionType.STRENGTH));
topShelf.add(factory.createPotion(PotionType.HEALING));
topShelf.add(factory.createPotion(PotionType.HEALING));
bottomShelf.add(factory.createPotion(PotionType.POISON));
bottomShelf.add(factory.createPotion(PotionType.POISON));
bottomShelf.add(factory.createPotion(PotionType.POISON));
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
bottomShelf.add(factory.createPotion(PotionType.HOLY_WATER));
}
public void enumerate() {

View File

@ -4,7 +4,7 @@ public class HealingPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel healed.");
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
}
}

View File

@ -4,7 +4,7 @@ public class HolyWaterPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel blessed.");
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
}
}

View File

@ -4,7 +4,7 @@ public class InvisibilityPotion implements Potion {
@Override
public void drink() {
System.out.println("You become invisible.");
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
}
}

View File

@ -4,7 +4,7 @@ public class PoisonPotion implements Potion {
@Override
public void drink() {
System.out.println("Urgh! This is poisonous.");
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
}
}

View File

@ -16,18 +16,23 @@ public class PotionFactory {
switch (type) {
case HEALING:
potion = new HealingPotion();
potions.put(type, potion);
break;
case HOLY_WATER:
potion = new HolyWaterPotion();
potions.put(type, potion);
break;
case INVISIBILITY:
potion = new InvisibilityPotion();
potions.put(type, potion);
break;
case POISON:
potion = new PoisonPotion();
potions.put(type, potion);
break;
case STRENGTH:
potion = new StrengthPotion();
potions.put(type, potion);
break;
default:
break;

View File

@ -4,7 +4,7 @@ public class StrengthPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel strong.");
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
}
}