docs: updates to several patterns

This commit is contained in:
Ilkka Seppälä
2024-05-28 19:46:39 +03:00
parent 4652842c94
commit 584e949714
70 changed files with 347 additions and 479 deletions
+7 -5
View File
@@ -32,13 +32,15 @@ Wikipedia says
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:
First of all, we have different `Potion` types:
```java
public interface Potion {
void drink();
}
```
```java
@Slf4j
public class HealingPotion implements Potion {
@Override
@@ -46,7 +48,9 @@ public class HealingPotion implements Potion {
LOGGER.info("You feel healed. (Potion={})", System.identityHashCode(this));
}
}
```
```java
@Slf4j
public class HolyWaterPotion implements Potion {
@Override
@@ -54,7 +58,9 @@ public class HolyWaterPotion implements Potion {
LOGGER.info("You feel blessed. (Potion={})", System.identityHashCode(this));
}
}
```
```java
@Slf4j
public class InvisibilityPotion implements Potion {
@Override
@@ -170,10 +176,6 @@ Program output:
09:02:52.734 [main] INFO com.iluwatar.flyweight.HolyWaterPotion -- You feel blessed. (Potion=1689843956)
```
## Class diagram
![Flyweight](./etc/flyweight.urm.png "Flyweight pattern class diagram")
## Applicability
The Flyweight pattern's effectiveness depends heavily on how and where it's used. Apply the Flyweight pattern when all the following are true: