added flyweight sample
This commit is contained in:
parent
8725969908
commit
3fcca577f8
23
flyweight/pom.xml
Normal file
23
flyweight/pom.xml
Normal 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>
|
49
flyweight/src/main/java/com/iluwatar/AlchemistShop.java
Normal file
49
flyweight/src/main/java/com/iluwatar/AlchemistShop.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
10
flyweight/src/main/java/com/iluwatar/App.java
Normal file
10
flyweight/src/main/java/com/iluwatar/App.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
AlchemistShop alchemistShop = new AlchemistShop();
|
||||||
|
alchemistShop.enumerate();
|
||||||
|
}
|
||||||
|
}
|
10
flyweight/src/main/java/com/iluwatar/HealingPotion.java
Normal file
10
flyweight/src/main/java/com/iluwatar/HealingPotion.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class HealingPotion implements Potion {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drink() {
|
||||||
|
System.out.println("You feel healed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
flyweight/src/main/java/com/iluwatar/HolyWaterPotion.java
Normal file
10
flyweight/src/main/java/com/iluwatar/HolyWaterPotion.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class HolyWaterPotion implements Potion {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drink() {
|
||||||
|
System.out.println("You feel blessed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
flyweight/src/main/java/com/iluwatar/InvisibilityPotion.java
Normal file
10
flyweight/src/main/java/com/iluwatar/InvisibilityPotion.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class InvisibilityPotion implements Potion {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drink() {
|
||||||
|
System.out.println("You become invisible.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
flyweight/src/main/java/com/iluwatar/PoisonPotion.java
Normal file
10
flyweight/src/main/java/com/iluwatar/PoisonPotion.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class PoisonPotion implements Potion {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drink() {
|
||||||
|
System.out.println("Urgh! This is poisonous.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
7
flyweight/src/main/java/com/iluwatar/Potion.java
Normal file
7
flyweight/src/main/java/com/iluwatar/Potion.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public interface Potion {
|
||||||
|
|
||||||
|
public void drink();
|
||||||
|
|
||||||
|
}
|
39
flyweight/src/main/java/com/iluwatar/PotionFactory.java
Normal file
39
flyweight/src/main/java/com/iluwatar/PotionFactory.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
flyweight/src/main/java/com/iluwatar/PotionType.java
Normal file
11
flyweight/src/main/java/com/iluwatar/PotionType.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public enum PotionType {
|
||||||
|
|
||||||
|
HEALING,
|
||||||
|
INVISIBILITY,
|
||||||
|
STRENGTH,
|
||||||
|
HOLY_WATER,
|
||||||
|
POISON;
|
||||||
|
|
||||||
|
}
|
10
flyweight/src/main/java/com/iluwatar/StrengthPotion.java
Normal file
10
flyweight/src/main/java/com/iluwatar/StrengthPotion.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class StrengthPotion implements Potion {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drink() {
|
||||||
|
System.out.println("You feel strong.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
3
pom.xml
3
pom.xml
@ -28,6 +28,7 @@
|
|||||||
<module>composite</module>
|
<module>composite</module>
|
||||||
<module>decorator</module>
|
<module>decorator</module>
|
||||||
<module>facade</module>
|
<module>facade</module>
|
||||||
|
<module>flyweight</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -45,4 +46,4 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
Loading…
x
Reference in New Issue
Block a user