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
+38-17
Original file line number
Diff line number
Diff line change
@@ -10,25 +10,32 @@ tags:
10
10
---
11
11
12
12
## Intent
13
-
Use sharing to support large numbers of fine-grained objects
14
-
efficiently.
13
+
14
+
Use sharing to support large numbers of fine-grained objects efficiently.
15
15
16
16
## Explanation
17
+
17
18
Real world example
18
19
19
-
> 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.
20
+
> Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is
21
+
> no need to create new object for each of them. Instead one object instance can represent multiple
22
+
> shelf items so memory footprint remains small.
20
23
21
24
In plain words
22
25
23
-
> It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects.
26
+
> It is used to minimize memory usage or computational expenses by sharing as much as possible with
27
+
> similar objects.
24
28
25
29
Wikipedia says
26
30
27
-
> 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
+
> In computer programming, flyweight is a software design pattern. A flyweight is an object that
32
+
> minimizes memory use by sharing as much data as possible with other similar objects; it is a way
33
+
> to use objects in large numbers when a simple repeated representation would use an unacceptable
34
+
> amount of memory.
28
35
29
36
**Programmatic example**
30
37
31
-
Translating our alchemist shop example from above. First of all we have different potion types
38
+
Translating our alchemist shop example from above. First of all we have different potion types:
32
39
33
40
```java
34
41
publicinterfacePotion {
@@ -60,7 +67,7 @@ public class InvisibilityPotion implements Potion {
60
67
}
61
68
```
62
69
63
-
Then the actual Flyweight object which is the factory for creating potions
70
+
Then the actual Flyweight class `PotionFactory`, which is the factory for creating potions.
64
71
65
72
```java
66
73
publicclassPotionFactory {
@@ -96,7 +103,7 @@ public class PotionFactory {
96
103
}
97
104
```
98
105
99
-
And it can be used as below
106
+
And it can be used as below:
100
107
101
108
```java
102
109
var factory =newPotionFactory();
@@ -108,19 +115,33 @@ factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potio
108
115
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
109
116
```
110
117
118
+
Program output:
119
+
120
+
```java
121
+
You become invisible. (Potion=6566818)
122
+
You feel healed. (Potion=648129364)
123
+
You become invisible. (Potion=6566818)
124
+
You feel blessed. (Potion=1104106489)
125
+
You feel blessed. (Potion=1104106489)
126
+
You feel healed. (Potion=648129364)
127
+
```
128
+
111
129
## Class diagram
130
+
112
131

113
132
114
133
## Applicability
115
-
The Flyweight pattern's effectiveness depends heavily on how
116
-
and where it's used. Apply the Flyweight pattern when all of the following are
117
-
true
118
-
119
-
* an application uses a large number of objects
120
-
* storage costs are high because of the sheer quantity of objects
121
-
* most object state can be made extrinsic
122
-
* many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed
123
-
* the application doesn't depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.
134
+
135
+
The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the
136
+
Flyweight pattern when all of the following are true:
137
+
138
+
* An application uses a large number of objects.
139
+
* Storage costs are high because of the sheer quantity of objects.
140
+
* Most object state can be made extrinsic.
141
+
* Many groups of objects may be replaced by relatively few shared objects once extrinsic state is
142
+
removed.
143
+
* The application doesn't depend on object identity. Since flyweight objects may be shared, identity
144
+
tests will return true for conceptually distinct objects.
0 commit comments