You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: flyweight/README.md
+77-23Lines changed: 77 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ permalink: /patterns/flyweight/
6
6
categories: Structural
7
7
language: en
8
8
tags:
9
-
- Gang Of Four
9
+
- Gang of Four
10
10
- Performance
11
11
---
12
12
@@ -16,11 +16,11 @@ Use sharing to support large numbers of fine-grained objects efficiently.
16
16
17
17
## Explanation
18
18
19
-
Realworld example
19
+
Real-world example
20
20
21
21
> Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is
22
-
> no need to create new object for each of them. Instead one object instance can represent multiple
23
-
> shelf items so memory footprint remains small.
22
+
> no need to create a new object for each of them. Instead, one object instance can represent
23
+
> multiple shelf items so the memory footprint remains small.
24
24
25
25
In plain words
26
26
@@ -36,7 +36,7 @@ Wikipedia says
36
36
37
37
**Programmatic example**
38
38
39
-
Translating our alchemist shop example from above. First of all we have different potion types:
39
+
Translating our alchemist shop example from above. First of all, we have different potion types:
40
40
41
41
```java
42
42
publicinterfacePotion {
@@ -104,27 +104,81 @@ public class PotionFactory {
104
104
}
105
105
```
106
106
107
-
And it can be used as below:
107
+
`AlchemistShop` contains two shelves of magic potions. The potions are created using the
108
+
aforementioned `PotionFactory`.
108
109
109
110
```java
110
-
var factory =newPotionFactory();
111
-
factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818)
112
-
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
113
-
factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818)
114
-
factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potion=1104106489)
115
-
factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potion=1104106489)
116
-
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
111
+
@Slf4j
112
+
publicclassAlchemistShop {
113
+
114
+
privatefinalList<Potion> topShelf;
115
+
privatefinalList<Potion> bottomShelf;
116
+
117
+
publicAlchemistShop() {
118
+
var factory =newPotionFactory();
119
+
topShelf =List.of(
120
+
factory.createPotion(PotionType.INVISIBILITY),
121
+
factory.createPotion(PotionType.INVISIBILITY),
122
+
factory.createPotion(PotionType.STRENGTH),
123
+
factory.createPotion(PotionType.HEALING),
124
+
factory.createPotion(PotionType.INVISIBILITY),
125
+
factory.createPotion(PotionType.STRENGTH),
126
+
factory.createPotion(PotionType.HEALING),
127
+
factory.createPotion(PotionType.HEALING)
128
+
);
129
+
bottomShelf =List.of(
130
+
factory.createPotion(PotionType.POISON),
131
+
factory.createPotion(PotionType.POISON),
132
+
factory.createPotion(PotionType.POISON),
133
+
factory.createPotion(PotionType.HOLY_WATER),
134
+
factory.createPotion(PotionType.HOLY_WATER)
135
+
);
136
+
}
137
+
138
+
publicfinalList<Potion>getTopShelf() {
139
+
returnList.copyOf(this.topShelf);
140
+
}
141
+
142
+
publicfinalList<Potion>getBottomShelf() {
143
+
returnList.copyOf(this.bottomShelf);
144
+
}
145
+
146
+
publicvoiddrinkPotions() {
147
+
LOGGER.info("Drinking top shelf potions\n");
148
+
topShelf.forEach(Potion::drink);
149
+
LOGGER.info("Drinking bottom shelf potions\n");
150
+
bottomShelf.forEach(Potion::drink);
151
+
}
152
+
}
153
+
```
154
+
155
+
In our scenario, a brave visitor enters the alchemist shop and drinks all the potions.
156
+
157
+
```java
158
+
// create the alchemist shop with the potions
159
+
var alchemistShop =newAlchemistShop();
160
+
// a brave visitor enters the alchemist shop and drinks all the potions
161
+
alchemistShop.drinkPotions();
117
162
```
118
163
119
164
Program output:
120
165
121
166
```java
122
-
You become invisible. (Potion=6566818)
123
-
You feel healed. (Potion=648129364)
124
-
You become invisible. (Potion=6566818)
125
-
You feel blessed. (Potion=1104106489)
126
-
You feel blessed. (Potion=1104106489)
127
-
You feel healed. (Potion=648129364)
167
+
Drinking top shelf potions
168
+
You become invisible. (Potion=1509514333)
169
+
You become invisible. (Potion=1509514333)
170
+
You feel strong. (Potion=739498517)
171
+
You feel healed. (Potion=125130493)
172
+
You become invisible. (Potion=1509514333)
173
+
You feel strong. (Potion=739498517)
174
+
You feel healed. (Potion=125130493)
175
+
You feel healed. (Potion=125130493)
176
+
Drinking bottom shelf potions
177
+
Urgh!This is poisonous. (Potion=166239592)
178
+
Urgh!This is poisonous. (Potion=166239592)
179
+
Urgh!This is poisonous. (Potion=166239592)
180
+
You feel blessed. (Potion=991505714)
181
+
You feel blessed. (Potion=991505714)
128
182
```
129
183
130
184
## Class diagram
@@ -138,13 +192,13 @@ Flyweight pattern when all of the following are true:
138
192
139
193
* An application uses a large number of objects.
140
194
* Storage costs are high because of the sheer quantity of objects.
141
-
* Most object state can be made extrinsic.
142
-
* Many groups of objects may be replaced by relatively few shared objects once extrinsic state is
143
-
removed.
195
+
* Most of the object state can be made extrinsic.
196
+
* Many groups of objects may be replaced by relatively few shared objects once the extrinsic state
197
+
is removed.
144
198
* The application doesn't depend on object identity. Since flyweight objects may be shared, identity
145
199
tests will return true for conceptually distinct objects.
146
200
147
-
## Real world examples
201
+
## Known uses
148
202
149
203
*[java.lang.Integer#valueOf(int)](http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#valueOf%28int%29) and similarly for Byte, Character and other wrapped types.
0 commit comments