Formatted all files to the same standard
This commit is contained in:
@ -6,13 +6,13 @@ import java.util.List;
|
||||
/**
|
||||
*
|
||||
* The class that needs many objects.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class AlchemistShop {
|
||||
|
||||
List<Potion> topShelf;
|
||||
List<Potion> bottomShelf;
|
||||
|
||||
|
||||
public AlchemistShop() {
|
||||
topShelf = new ArrayList<>();
|
||||
bottomShelf = new ArrayList<>();
|
||||
@ -20,9 +20,9 @@ public class AlchemistShop {
|
||||
}
|
||||
|
||||
private void fillShelves() {
|
||||
|
||||
|
||||
PotionFactory factory = new PotionFactory();
|
||||
|
||||
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.INVISIBILITY));
|
||||
topShelf.add(factory.createPotion(PotionType.STRENGTH));
|
||||
@ -31,27 +31,27 @@ public class AlchemistShop {
|
||||
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() {
|
||||
|
||||
System.out.println("Enumerating top shelf potions\n");
|
||||
|
||||
for (Potion p: topShelf) {
|
||||
|
||||
for (Potion p : topShelf) {
|
||||
p.drink();
|
||||
}
|
||||
|
||||
|
||||
System.out.println("\nEnumerating bottom shelf potions\n");
|
||||
|
||||
for (Potion p: bottomShelf) {
|
||||
|
||||
for (Potion p : bottomShelf) {
|
||||
p.drink();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Flyweight (PotionFactory) is useful when there is plethora of objects
|
||||
* (Potion). It provides means to decrease resource usage by sharing object
|
||||
* instances.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AlchemistShop alchemistShop = new AlchemistShop();
|
||||
alchemistShop.enumerate();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
AlchemistShop alchemistShop = new AlchemistShop();
|
||||
alchemistShop.enumerate();
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class HealingPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel healed. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class HolyWaterPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel blessed. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class InvisibilityPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
|
||||
System.out.println("You become invisible. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class PoisonPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
|
||||
System.out.println("Urgh! This is poisonous. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* Interface for objects.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface Potion {
|
||||
|
||||
public void drink();
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,16 +5,16 @@ import java.util.EnumMap;
|
||||
/**
|
||||
*
|
||||
* Flyweight.
|
||||
*
|
||||
*
|
||||
*/
|
||||
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) {
|
||||
@ -45,5 +45,5 @@ public class PotionFactory {
|
||||
}
|
||||
return potion;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,10 +2,6 @@ package com.iluwatar;
|
||||
|
||||
public enum PotionType {
|
||||
|
||||
HEALING,
|
||||
INVISIBILITY,
|
||||
STRENGTH,
|
||||
HOLY_WATER,
|
||||
POISON;
|
||||
|
||||
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON;
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ public class StrengthPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel strong. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user