added flyweight sample

This commit is contained in:
Ilkka Seppala 2014-08-16 19:40:39 +03:00
parent 8725969908
commit 3fcca577f8
12 changed files with 191 additions and 1 deletions

23
flyweight/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<artifactId>flyweight</artifactId>
<version>1.0-SNAPSHOT</version>
<name>flyweight</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,49 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.List;
public class AlchemistShop {
List<Potion> topShelf;
List<Potion> 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();
}
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class App
{
public static void main( String[] args )
{
AlchemistShop alchemistShop = new AlchemistShop();
alchemistShop.enumerate();
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class HealingPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel healed.");
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class HolyWaterPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel blessed.");
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class InvisibilityPotion implements Potion {
@Override
public void drink() {
System.out.println("You become invisible.");
}
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class PoisonPotion implements Potion {
@Override
public void drink() {
System.out.println("Urgh! This is poisonous.");
}
}

View File

@ -0,0 +1,7 @@
package com.iluwatar;
public interface Potion {
public void drink();
}

View File

@ -0,0 +1,39 @@
package com.iluwatar;
import java.util.EnumMap;
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();
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;
}
}

View File

@ -0,0 +1,11 @@
package com.iluwatar;
public enum PotionType {
HEALING,
INVISIBILITY,
STRENGTH,
HOLY_WATER,
POISON;
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class StrengthPotion implements Potion {
@Override
public void drink() {
System.out.println("You feel strong.");
}
}

View File

@ -28,6 +28,7 @@
<module>composite</module>
<module>decorator</module>
<module>facade</module>
<module>flyweight</module>
</modules>
<build>
@ -45,4 +46,4 @@
</plugins>
</build>
</project>
</project>