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
+94-1Lines changed: 94 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,100 @@ tags:
16
16
Use sharing to support large numbers of fine-grained objects
17
17
efficiently.
18
18
19
-

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);
0 commit comments