diff --git a/flyweight/pom.xml b/flyweight/pom.xml new file mode 100644 index 000000000..3cc3244aa --- /dev/null +++ b/flyweight/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + com.iluwatar + flyweight + 1.0-SNAPSHOT + flyweight + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/flyweight/src/main/java/com/iluwatar/AlchemistShop.java b/flyweight/src/main/java/com/iluwatar/AlchemistShop.java new file mode 100644 index 000000000..d2cee5765 --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/AlchemistShop.java @@ -0,0 +1,49 @@ +package com.iluwatar; + +import java.util.ArrayList; +import java.util.List; + +public class AlchemistShop { + + List topShelf; + List bottomShelf; + + public AlchemistShop() { + topShelf = new ArrayList<>(); + bottomShelf = new ArrayList<>(); + 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()); + bottomShelf.add(new PoisonPotion()); + bottomShelf.add(new PoisonPotion()); + bottomShelf.add(new HolyWaterPotion()); + bottomShelf.add(new HolyWaterPotion()); + } + + public void enumerate() { + + System.out.println("Enumerating top shelf potions\n"); + + for (Potion p: topShelf) { + p.drink(); + } + + System.out.println("\nEnumerating bottom shelf potions\n"); + + for (Potion p: bottomShelf) { + p.drink(); + } + + } +} diff --git a/flyweight/src/main/java/com/iluwatar/App.java b/flyweight/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000..90b7131f6 --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/App.java @@ -0,0 +1,10 @@ +package com.iluwatar; + +public class App +{ + public static void main( String[] args ) + { + AlchemistShop alchemistShop = new AlchemistShop(); + alchemistShop.enumerate(); + } +} diff --git a/flyweight/src/main/java/com/iluwatar/HealingPotion.java b/flyweight/src/main/java/com/iluwatar/HealingPotion.java new file mode 100644 index 000000000..f4a82339a --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/HealingPotion.java @@ -0,0 +1,10 @@ +package com.iluwatar; + +public class HealingPotion implements Potion { + + @Override + public void drink() { + System.out.println("You feel healed."); + } + +} diff --git a/flyweight/src/main/java/com/iluwatar/HolyWaterPotion.java b/flyweight/src/main/java/com/iluwatar/HolyWaterPotion.java new file mode 100644 index 000000000..ba861f6be --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/HolyWaterPotion.java @@ -0,0 +1,10 @@ +package com.iluwatar; + +public class HolyWaterPotion implements Potion { + + @Override + public void drink() { + System.out.println("You feel blessed."); + } + +} diff --git a/flyweight/src/main/java/com/iluwatar/InvisibilityPotion.java b/flyweight/src/main/java/com/iluwatar/InvisibilityPotion.java new file mode 100644 index 000000000..f5f2bcec3 --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/InvisibilityPotion.java @@ -0,0 +1,10 @@ +package com.iluwatar; + +public class InvisibilityPotion implements Potion { + + @Override + public void drink() { + System.out.println("You become invisible."); + } + +} diff --git a/flyweight/src/main/java/com/iluwatar/PoisonPotion.java b/flyweight/src/main/java/com/iluwatar/PoisonPotion.java new file mode 100644 index 000000000..733f1234e --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/PoisonPotion.java @@ -0,0 +1,10 @@ +package com.iluwatar; + +public class PoisonPotion implements Potion { + + @Override + public void drink() { + System.out.println("Urgh! This is poisonous."); + } + +} diff --git a/flyweight/src/main/java/com/iluwatar/Potion.java b/flyweight/src/main/java/com/iluwatar/Potion.java new file mode 100644 index 000000000..5c0d48a32 --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/Potion.java @@ -0,0 +1,7 @@ +package com.iluwatar; + +public interface Potion { + + public void drink(); + +} diff --git a/flyweight/src/main/java/com/iluwatar/PotionFactory.java b/flyweight/src/main/java/com/iluwatar/PotionFactory.java new file mode 100644 index 000000000..6d067b7fa --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/PotionFactory.java @@ -0,0 +1,39 @@ +package com.iluwatar; + +import java.util.EnumMap; + +public class PotionFactory { + + private EnumMap 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(); + break; + case HOLY_WATER: + potion = new HolyWaterPotion(); + break; + case INVISIBILITY: + potion = new InvisibilityPotion(); + break; + case POISON: + potion = new PoisonPotion(); + break; + case STRENGTH: + potion = new StrengthPotion(); + break; + default: + break; + } + } + return potion; + } + +} diff --git a/flyweight/src/main/java/com/iluwatar/PotionType.java b/flyweight/src/main/java/com/iluwatar/PotionType.java new file mode 100644 index 000000000..ff4e392c9 --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/PotionType.java @@ -0,0 +1,11 @@ +package com.iluwatar; + +public enum PotionType { + + HEALING, + INVISIBILITY, + STRENGTH, + HOLY_WATER, + POISON; + +} diff --git a/flyweight/src/main/java/com/iluwatar/StrengthPotion.java b/flyweight/src/main/java/com/iluwatar/StrengthPotion.java new file mode 100644 index 000000000..1b90b0269 --- /dev/null +++ b/flyweight/src/main/java/com/iluwatar/StrengthPotion.java @@ -0,0 +1,10 @@ +package com.iluwatar; + +public class StrengthPotion implements Potion { + + @Override + public void drink() { + System.out.println("You feel strong."); + } + +} diff --git a/pom.xml b/pom.xml index 422ae2387..b3a47d5ec 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ composite decorator facade + flyweight @@ -45,4 +46,4 @@ - + \ No newline at end of file