50 lines
938 B
Java
Raw Normal View History

2014-08-16 19:40:39 +03:00
package com.iluwatar;
import java.util.EnumMap;
2014-08-31 10:16:48 +03:00
/**
*
* Flyweight.
*
*/
2014-08-16 19:40:39 +03:00
public class PotionFactory {
private EnumMap<PotionType, Potion> potions;
public PotionFactory() {
potions = new EnumMap<>(PotionType.class);
}
Potion createPotion(PotionType type) {
Potion potion = potions.get(type);
if (potion == null) {
switch (type) {
case HEALING:
potion = new HealingPotion();
2014-08-23 19:13:09 +03:00
potions.put(type, potion);
2014-08-16 19:40:39 +03:00
break;
case HOLY_WATER:
potion = new HolyWaterPotion();
2014-08-23 19:13:09 +03:00
potions.put(type, potion);
2014-08-16 19:40:39 +03:00
break;
case INVISIBILITY:
potion = new InvisibilityPotion();
2014-08-23 19:13:09 +03:00
potions.put(type, potion);
2014-08-16 19:40:39 +03:00
break;
case POISON:
potion = new PoisonPotion();
2014-08-23 19:13:09 +03:00
potions.put(type, potion);
2014-08-16 19:40:39 +03:00
break;
case STRENGTH:
potion = new StrengthPotion();
2014-08-23 19:13:09 +03:00
potions.put(type, potion);
2014-08-16 19:40:39 +03:00
break;
default:
break;
}
}
return potion;
}
}