Skip to content

Commit f3fd498

Browse files
committed
Update README.md
1 parent 1fbef60 commit f3fd498

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

flyweight/README.md

+38-17
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,32 @@ tags:
1010
---
1111

1212
## 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.
1515

1616
## Explanation
17+
1718
Real world example
1819

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.
2023
2124
In plain words
2225

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.
2428
2529
Wikipedia says
2630

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.
2835
2936
**Programmatic example**
3037

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:
3239

3340
```java
3441
public interface Potion {
@@ -60,7 +67,7 @@ public class InvisibilityPotion implements Potion {
6067
}
6168
```
6269

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.
6471

6572
```java
6673
public class PotionFactory {
@@ -96,7 +103,7 @@ public class PotionFactory {
96103
}
97104
```
98105

99-
And it can be used as below
106+
And it can be used as below:
100107

101108
```java
102109
var factory = new PotionFactory();
@@ -108,19 +115,33 @@ factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potio
108115
factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364)
109116
```
110117

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+
111129
## Class diagram
130+
112131
![alt text](./etc/flyweight.urm.png "Flyweight pattern class diagram")
113132

114133
## 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.
124145

125146
## Real world examples
126147

0 commit comments

Comments
 (0)