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

@ -15,20 +15,23 @@ public class AlchemistShop {
} }
private void fillShelves() { 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()); PotionFactory factory = new PotionFactory();
bottomShelf.add(new PoisonPotion());
bottomShelf.add(new PoisonPotion()); topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
bottomShelf.add(new HolyWaterPotion()); topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
bottomShelf.add(new HolyWaterPotion()); 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() { public void enumerate() {

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

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

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

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

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

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