mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 10:15:44 +00:00
docs: update flyweight
This commit is contained in:
1 parent
248f955f16
commit
774d6820a2
1 file changed
+29
-25
+29
-25
@@ -18,7 +18,7 @@ The Flyweight pattern's primary intent is to reduce the number of objects create
|
|||||||
|
|
||||||
Real-world example
|
Real-world example
|
||||||
|
|
||||||
> Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create a new object for each of them. Instead, one object instance can represent multiple shelf items so the memory footprint remains small.
|
> A real-world example of the Flyweight design pattern is in a document editor like Microsoft Word or Google Docs. In such applications, each character in a document could potentially be a separate object, which would be highly inefficient in terms of memory usage. Instead, the Flyweight pattern can be used to share character objects. For instance, all instances of the letter 'A' can share a single 'A' object with its intrinsic state (e.g., the shape of the character). The extrinsic state, such as the position, font, and color, can be stored separately and applied as needed. This way, the application efficiently manages memory by reusing existing objects for characters that appear multiple times.
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
@@ -30,7 +30,9 @@ Wikipedia says
|
|||||||
|
|
||||||
**Programmatic example**
|
**Programmatic example**
|
||||||
|
|
||||||
Translating our alchemist shop example from above. First of all, we have different potion types:
|
Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create a new object for each of them. Instead, one object instance can represent multiple shelf items so the memory footprint remains small.
|
||||||
|
|
||||||
|
First of all, we have different potion types:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public interface Potion {
|
public interface Potion {
|
||||||
@@ -140,35 +142,37 @@ public class AlchemistShop {
|
|||||||
In our scenario, a brave visitor enters the alchemist shop and drinks all the potions.
|
In our scenario, a brave visitor enters the alchemist shop and drinks all the potions.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// create the alchemist shop with the potions
|
public static void main(String[] args) {
|
||||||
var alchemistShop = new AlchemistShop();
|
// create the alchemist shop with the potions
|
||||||
// a brave visitor enters the alchemist shop and drinks all the potions
|
var alchemistShop = new AlchemistShop();
|
||||||
alchemistShop.drinkPotions();
|
// a brave visitor enters the alchemist shop and drinks all the potions
|
||||||
|
alchemistShop.drinkPotions();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output:
|
Program output:
|
||||||
|
|
||||||
```java
|
```
|
||||||
Drinking top shelf potions
|
09:02:52.731 [main] INFO com.iluwatar.flyweight.AlchemistShop -- Drinking top shelf potions
|
||||||
You become invisible. (Potion=1509514333)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.InvisibilityPotion -- You become invisible. (Potion=1395089624)
|
||||||
You become invisible. (Potion=1509514333)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.InvisibilityPotion -- You become invisible. (Potion=1395089624)
|
||||||
You feel strong. (Potion=739498517)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.StrengthPotion -- You feel strong. (Potion=1450821318)
|
||||||
You feel healed. (Potion=125130493)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.HealingPotion -- You feel healed. (Potion=668849042)
|
||||||
You become invisible. (Potion=1509514333)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.InvisibilityPotion -- You become invisible. (Potion=1395089624)
|
||||||
You feel strong. (Potion=739498517)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.StrengthPotion -- You feel strong. (Potion=1450821318)
|
||||||
You feel healed. (Potion=125130493)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.HealingPotion -- You feel healed. (Potion=668849042)
|
||||||
You feel healed. (Potion=125130493)
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.HealingPotion -- You feel healed. (Potion=668849042)
|
||||||
Drinking bottom shelf potions
|
09:02:52.733 [main] INFO com.iluwatar.flyweight.AlchemistShop -- Drinking bottom shelf potions
|
||||||
Urgh! This is poisonous. (Potion=166239592)
|
09:02:52.734 [main] INFO com.iluwatar.flyweight.PoisonPotion -- Urgh! This is poisonous. (Potion=2096057945)
|
||||||
Urgh! This is poisonous. (Potion=166239592)
|
09:02:52.734 [main] INFO com.iluwatar.flyweight.PoisonPotion -- Urgh! This is poisonous. (Potion=2096057945)
|
||||||
Urgh! This is poisonous. (Potion=166239592)
|
09:02:52.734 [main] INFO com.iluwatar.flyweight.PoisonPotion -- Urgh! This is poisonous. (Potion=2096057945)
|
||||||
You feel blessed. (Potion=991505714)
|
09:02:52.734 [main] INFO com.iluwatar.flyweight.HolyWaterPotion -- You feel blessed. (Potion=1689843956)
|
||||||
You feel blessed. (Potion=991505714)
|
09:02:52.734 [main] INFO com.iluwatar.flyweight.HolyWaterPotion -- You feel blessed. (Potion=1689843956)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
@@ -200,8 +204,8 @@ Trade-offs:
|
|||||||
|
|
||||||
## Related Patterns
|
## Related Patterns
|
||||||
|
|
||||||
[Composite](https://java-design-patterns.com/patterns/composite/): Often combined with Flyweight when the composites are shareable. Both are used to manage hierarchies and structures of objects.
|
* [Composite](https://java-design-patterns.com/patterns/composite/): Often combined with Flyweight when the composites are shareable. Both are used to manage hierarchies and structures of objects.
|
||||||
[State](https://java-design-patterns.com/patterns/state/): Can be used to manage state in a shared Flyweight object, distinguishing internal state (invariant) from external state (context-specific).
|
* [State](https://java-design-patterns.com/patterns/state/): Can be used to manage state in a shared Flyweight object, distinguishing internal state (invariant) from external state (context-specific).
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user