docs: improve flyweight

This commit is contained in:
Ilkka Seppälä
2024-04-21 18:23:22 +03:00
parent dabf40e1bb
commit 596f429aa0
2 changed files with 36 additions and 22 deletions
+2
View File
@@ -116,3 +116,5 @@ Trade-offs:
## Credits ## Credits
* [Flux - Application architecture for building user interfaces](http://facebook.github.io/flux/) * [Flux - Application architecture for building user interfaces](http://facebook.github.io/flux/)
* [Learning React: Modern Patterns for Developing React Apps](https://amzn.to/3Qdn9Pg)
* [Pro React](https://amzn.to/3xNRttK)
+34 -22
View File
@@ -3,33 +3,30 @@ title: Flyweight
category: Structural category: Structural
language: en language: en
tag: tag:
- Gang of Four - Gang of Four
- Performance - Memory management
- Object composition
- Optimization
- Performance
--- ---
## Intent ## Intent
Use sharing to support large numbers of fine-grained objects efficiently. The Flyweight pattern's primary intent is to reduce the number of objects created, decrease memory footprint and increase performance by sharing as much data as possible with similar objects.
## Explanation ## Explanation
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 > 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.
> 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.
In plain words In plain words
> It is used to minimize memory usage or computational expenses by sharing as much as possible with > It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects.
> similar objects.
Wikipedia says Wikipedia says
> In computer programming, flyweight is a software design pattern. A flyweight is an object that > 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.
> 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.
**Programmatic example** **Programmatic example**
@@ -101,8 +98,7 @@ public class PotionFactory {
} }
``` ```
`AlchemistShop` contains two shelves of magic potions. The potions are created using the `AlchemistShop` contains two shelves of magic potions. The potions are created using the aforementioned `PotionFactory`.
aforementioned `PotionFactory`.
```java ```java
@Slf4j @Slf4j
@@ -184,22 +180,38 @@ You feel blessed. (Potion=991505714)
## Applicability ## Applicability
The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the Flyweight pattern when all the following are true:
Flyweight pattern when all of the following are true:
* An application uses a large number of objects. * An application uses a large number of objects.
* Storage costs are high because of the sheer quantity of objects. * Storage costs are high because of the sheer quantity of objects.
* Most of the object state can be made extrinsic. * Most of the object state can be made extrinsic.
* Many groups of objects may be replaced by relatively few shared objects once the extrinsic state * Many groups of objects may be replaced by relatively few shared objects once the extrinsic state is removed.
is removed. * The application doesn't depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.
* The application doesn't depend on object identity. Since flyweight objects may be shared, identity
tests will return true for conceptually distinct objects.
## Known uses ## Known uses
* [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. * [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.
* Javas String class, which uses the Flyweight pattern internally to manage string literals.
* GUI applications, where objects like fonts or graphical components are shared rather than duplicated.
## Consequences
Benefits:
* Reduces the number of instances of an object, conserving memory.
* Centralizes state management, reducing the risk of inconsistent state.
Trade-offs:
* Increases complexity by adding the management layer for shared objects.
* Potential overhead in accessing shared objects if not well implemented.
## 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.
[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
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) * [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) * [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)