Skip to content

Commit c136618

Browse files
committed
iluwatar#590 Add explanation for Flyweight
1 parent bd4247e commit c136618

File tree

6 files changed

+95
-202
lines changed

6 files changed

+95
-202
lines changed

flyweight/README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,100 @@ tags:
1616
Use sharing to support large numbers of fine-grained objects
1717
efficiently.
1818

19-
![alt text](./etc/flyweight_1.png "Flyweight")
19+
## Explanation
20+
Real world example
21+
22+
> Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create new object for each of them. Instead one object instance can represent multiple shelf items so memory footprint remains small.
23+
24+
In plain words
25+
26+
> It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects.
27+
28+
Wikipedia says
29+
30+
> In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory.
31+
32+
**Programmatic example**
33+
34+
Translating our alchemist shop example from above. First of all we have different potion types
35+
36+
```
37+
public interface Potion {
38+
void drink();
39+
}
40+
41+
public class HealingPotion implements Potion {
42+
private static final Logger LOGGER = LoggerFactory.getLogger(HealingPotion.class);
43+
@Override
44+
public void drink() {
45+
LOGGER.info("You feel healed. (Potion={})", System.identityHashCode(this));
46+
}
47+
}
48+
49+
public class HolyWaterPotion implements Potion {
50+
private static final Logger LOGGER = LoggerFactory.getLogger(HolyWaterPotion.class);
51+
@Override
52+
public void drink() {
53+
LOGGER.info("You feel blessed. (Potion={})", System.identityHashCode(this));
54+
}
55+
}
56+
57+
public class InvisibilityPotion implements Potion {
58+
private static final Logger LOGGER = LoggerFactory.getLogger(InvisibilityPotion.class);
59+
@Override
60+
public void drink() {
61+
LOGGER.info("You become invisible. (Potion={})", System.identityHashCode(this));
62+
}
63+
}
64+
```
65+
66+
Then the actual Flyweight object which is the factory for creating potions
67+
68+
```
69+
public class PotionFactory {
70+
71+
private final Map<PotionType, Potion> potions;
72+
73+
public PotionFactory() {
74+
potions = new EnumMap<>(PotionType.class);
75+
}
76+
77+
Potion createPotion(PotionType type) {
78+
Potion potion = potions.get(type);
79+
if (potion == null) {
80+
switch (type) {
81+
case HEALING:
82+
potion = new HealingPotion();
83+
potions.put(type, potion);
84+
break;
85+
case HOLY_WATER:
86+
potion = new HolyWaterPotion();
87+
potions.put(type, potion);
88+
break;
89+
case INVISIBILITY:
90+
potion = new InvisibilityPotion();
91+
potions.put(type, potion);
92+
break;
93+
default:
94+
break;
95+
}
96+
}
97+
return potion;
98+
}
99+
}
100+
```
101+
102+
And it can be used as below
103+
104+
```
105+
PotionFactory factory = new PotionFactory();
106+
factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818)
107+
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
108+
factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818)
109+
factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potion=1104106489)
110+
factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potion=1104106489)
111+
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
112+
```
20113

21114
## Applicability
22115
The Flyweight pattern's effectiveness depends heavily on how

flyweight/etc/flyweight.png

-31.2 KB
Binary file not shown.

flyweight/etc/flyweight.ucls

Lines changed: 0 additions & 135 deletions
This file was deleted.

flyweight/etc/flyweight.urm.puml

Lines changed: 0 additions & 66 deletions
This file was deleted.

flyweight/etc/flyweight_1.png

-62.4 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@
471471
<param>composite</param>
472472
<param>decorator</param>
473473
<param>facade</param>
474+
<param>flyweight</param>
474475
</skipForProjects>
475476
</configuration>
476477
</plugin>

0 commit comments

Comments
 (0)