mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 12:58:37 +00:00
docs: improve flyweight
This commit is contained in:
@@ -116,3 +116,5 @@ Trade-offs:
|
||||
## Credits
|
||||
|
||||
* [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
@@ -3,33 +3,30 @@ title: Flyweight
|
||||
category: Structural
|
||||
language: en
|
||||
tag:
|
||||
- Gang of Four
|
||||
- Performance
|
||||
- Gang of Four
|
||||
- Memory management
|
||||
- Object composition
|
||||
- Optimization
|
||||
- Performance
|
||||
---
|
||||
|
||||
## 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
|
||||
|
||||
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.
|
||||
> 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.
|
||||
|
||||
In plain words
|
||||
|
||||
> It is used to minimize memory usage or computational expenses by sharing as much as possible with
|
||||
> similar objects.
|
||||
> It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> 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.
|
||||
> 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.
|
||||
|
||||
**Programmatic example**
|
||||
|
||||
@@ -101,8 +98,7 @@ public class PotionFactory {
|
||||
}
|
||||
```
|
||||
|
||||
`AlchemistShop` contains two shelves of magic potions. The potions are created using the
|
||||
aforementioned `PotionFactory`.
|
||||
`AlchemistShop` contains two shelves of magic potions. The potions are created using the aforementioned `PotionFactory`.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@@ -184,22 +180,38 @@ You feel blessed. (Potion=991505714)
|
||||
|
||||
## Applicability
|
||||
|
||||
The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the
|
||||
Flyweight pattern when all of the following are true:
|
||||
The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the Flyweight pattern when all the following are true:
|
||||
|
||||
* An application uses a large number of objects.
|
||||
* Storage costs are high because of the sheer quantity of objects.
|
||||
* 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
|
||||
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.
|
||||
* Many groups of objects may be replaced by relatively few shared objects once the extrinsic state 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.
|
||||
|
||||
## 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’s 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
|
||||
|
||||
* [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)
|
||||
* [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)
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
|
||||
|
||||
Reference in New Issue
Block a user