File tree Expand file tree Collapse file tree 12 files changed +191
-1
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 12 files changed +191
-1
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" ?>
2+ <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"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
4+ <modelVersion >4.0.0</modelVersion >
5+ <parent >
6+ <groupId >com.iluwatar</groupId >
7+ <artifactId >java-design-patterns</artifactId >
8+ <version >1.0-SNAPSHOT</version >
9+ </parent >
10+ <groupId >com.iluwatar</groupId >
11+ <artifactId >flyweight</artifactId >
12+ <version >1.0-SNAPSHOT</version >
13+ <name >flyweight</name >
14+ <url >http://maven.apache.org</url >
15+ <dependencies >
16+ <dependency >
17+ <groupId >junit</groupId >
18+ <artifactId >junit</artifactId >
19+ <version >3.8.1</version >
20+ <scope >test</scope >
21+ </dependency >
22+ </dependencies >
23+ </project >
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ public class AlchemistShop {
7+
8+ List <Potion > topShelf ;
9+ List <Potion > bottomShelf ;
10+
11+ public AlchemistShop () {
12+ topShelf = new ArrayList <>();
13+ bottomShelf = new ArrayList <>();
14+ fillShelves ();
15+ }
16+
17+ private void fillShelves () {
18+ topShelf .add (new InvisibilityPotion ());
19+ topShelf .add (new InvisibilityPotion ());
20+ topShelf .add (new StrengthPotion ());
21+ topShelf .add (new HealingPotion ());
22+ topShelf .add (new InvisibilityPotion ());
23+ topShelf .add (new StrengthPotion ());
24+ topShelf .add (new HealingPotion ());
25+ topShelf .add (new HealingPotion ());
26+
27+ bottomShelf .add (new PoisonPotion ());
28+ bottomShelf .add (new PoisonPotion ());
29+ bottomShelf .add (new PoisonPotion ());
30+ bottomShelf .add (new HolyWaterPotion ());
31+ bottomShelf .add (new HolyWaterPotion ());
32+ }
33+
34+ public void enumerate () {
35+
36+ System .out .println ("Enumerating top shelf potions\n " );
37+
38+ for (Potion p : topShelf ) {
39+ p .drink ();
40+ }
41+
42+ System .out .println ("\n Enumerating bottom shelf potions\n " );
43+
44+ for (Potion p : bottomShelf ) {
45+ p .drink ();
46+ }
47+
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class App
4+ {
5+ public static void main ( String [] args )
6+ {
7+ AlchemistShop alchemistShop = new AlchemistShop ();
8+ alchemistShop .enumerate ();
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class HealingPotion implements Potion {
4+
5+ @ Override
6+ public void drink () {
7+ System .out .println ("You feel healed." );
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class HolyWaterPotion implements Potion {
4+
5+ @ Override
6+ public void drink () {
7+ System .out .println ("You feel blessed." );
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class InvisibilityPotion implements Potion {
4+
5+ @ Override
6+ public void drink () {
7+ System .out .println ("You become invisible." );
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class PoisonPotion implements Potion {
4+
5+ @ Override
6+ public void drink () {
7+ System .out .println ("Urgh! This is poisonous." );
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface Potion {
4+
5+ public void drink ();
6+
7+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ import java .util .EnumMap ;
4+
5+ public class PotionFactory {
6+
7+ private EnumMap <PotionType , Potion > potions ;
8+
9+ public PotionFactory () {
10+ potions = new EnumMap <>(PotionType .class );
11+ }
12+
13+ Potion createPotion (PotionType type ) {
14+ Potion potion = potions .get (type );
15+ if (potion == null ) {
16+ switch (type ) {
17+ case HEALING :
18+ potion = new HealingPotion ();
19+ break ;
20+ case HOLY_WATER :
21+ potion = new HolyWaterPotion ();
22+ break ;
23+ case INVISIBILITY :
24+ potion = new InvisibilityPotion ();
25+ break ;
26+ case POISON :
27+ potion = new PoisonPotion ();
28+ break ;
29+ case STRENGTH :
30+ potion = new StrengthPotion ();
31+ break ;
32+ default :
33+ break ;
34+ }
35+ }
36+ return potion ;
37+ }
38+
39+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public enum PotionType {
4+
5+ HEALING ,
6+ INVISIBILITY ,
7+ STRENGTH ,
8+ HOLY_WATER ,
9+ POISON ;
10+
11+ }
You can’t perform that action at this time.
0 commit comments