Skip to content

Commit 66538dd

Browse files
authored
docs: update Dirty Flag docs (#2908)
1 parent 317f9c3 commit 66538dd

File tree

4 files changed

+125
-36
lines changed

4 files changed

+125
-36
lines changed

dirty-flag/README.md

Lines changed: 121 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,139 @@ title: Dirty Flag
33
category: Behavioral
44
language: en
55
tag:
6-
- Game programming
7-
- Performance
6+
- Game programming
7+
- Performance
8+
- Resource management
9+
- State tracking
810
---
911

1012
## Also known as
11-
* IsDirty pattern
13+
14+
* Change Tracking
15+
* Is-Modified Flag
1216

1317
## Intent
14-
To avoid expensive re-acquisition of resources. The resources retain their identity, are kept in some
15-
fast-access storage, and are re-used to avoid having to acquire them again.
18+
19+
The Dirty Flag design pattern is employed to avoid unnecessary computations or resource-heavy operations by maintaining a boolean flag that tracks whether the state of an object has changed ('dirty') or remains unchanged ('clean'). This flag, when set, indicates that a particular operation, such as recalculating or refreshing data, needs to be performed again to reflect the updated state.
20+
21+
## Explanation
22+
23+
Real world example
24+
25+
> Imagine a library with an electronic catalog system that tracks which books are checked out and returned. Each book record has a "dirty flag" that gets marked whenever a book is checked out or returned. At the end of each day, the library staff reviews only those records marked as "dirty" to update the physical inventory, instead of checking every single book in the library. This system significantly reduces the effort and time required for daily inventory checks by focusing only on the items that have changed status, analogous to how the Dirty Flag design pattern minimizes resource-intensive operations by performing them only when necessary.
26+
27+
In plain words
28+
29+
> The Dirty Flag design pattern minimizes unnecessary operations by using a flag to track when an object's state has changed and an update is needed.
30+
31+
Wikipedia says
32+
33+
> A dirty bit or modified bit is a bit that is associated with a block of computer memory and indicates whether the corresponding block of memory has been modified. The dirty bit is set when the processor writes to (modifies) this memory. The bit indicates that its associated block of memory has been modified and has not been saved to storage yet. When a block of memory is to be replaced, its corresponding dirty bit is checked to see if the block needs to be written back to secondary memory before being replaced or if it can simply be removed. Dirty bits are used by the CPU cache and in the page replacement algorithms of an operating system.
34+
35+
**Programmatic Example**
36+
37+
The DataFetcher class is responsible for fetching data from a file. It has a dirty flag that indicates whether the data in the file has changed since the last fetch.
38+
39+
```java
40+
public class DataFetcher {
41+
private long lastFetched;
42+
private boolean isDirty = true;
43+
// ...
44+
}
45+
```
46+
47+
The DataFetcher class has a fetch method that checks the dirty flag before fetching data. If the flag is true, it fetches the data from the file and sets the flag to false. If the flag is false, it returns the previously fetched data.
48+
49+
```java
50+
public List<String> fetch() {
51+
if (!isDirty) {
52+
return data;
53+
}
54+
data = fetchFromDatabase();
55+
isDirty = false;
56+
return data;
57+
}
58+
```
59+
60+
The World class uses the DataFetcher to fetch data. It has a fetch method that calls the fetch method of the DataFetcher.
61+
62+
```java
63+
public class World {
64+
private final DataFetcher fetcher = new DataFetcher();
65+
66+
public List<String> fetch() {
67+
return fetcher.fetch();
68+
}
69+
}
70+
```
71+
72+
The App class contains the main method that demonstrates the use of the Dirty Flag pattern. It creates a World object and fetches data from it in a loop. The World object uses the DataFetcher to fetch data, and the DataFetcher only fetches data from the file if the dirty flag is true.
73+
74+
```java
75+
public class App {
76+
public void run() {
77+
final var executorService = Executors.newSingleThreadScheduledExecutor();
78+
executorService.scheduleAtFixedRate(new Runnable() {
79+
final World world = new World();
80+
81+
@Override
82+
public void run() {
83+
var countries = world.fetch();
84+
// ...
85+
}
86+
}, 0, 15, TimeUnit.SECONDS); // Run at every 15 seconds.
87+
}
88+
}
89+
```
90+
91+
The program output is as follows:
92+
93+
```
94+
20:51:42.490 [pool-1-thread-1] INFO com.iluwatar.dirtyflag.DataFetcher -- world.txt is dirty! Re-fetching file content...
95+
20:51:42.494 [pool-1-thread-1] INFO com.iluwatar.dirtyflag.App -- Our world currently has the following countries:-
96+
20:51:42.494 [pool-1-thread-1] INFO com.iluwatar.dirtyflag.App -- UNITED_KINGDOM
97+
20:51:42.494 [pool-1-thread-1] INFO com.iluwatar.dirtyflag.App -- MALAYSIA
98+
20:51:42.494 [pool-1-thread-1] INFO com.iluwatar.dirtyflag.App -- UNITED_STATES
99+
```
16100

17101
## Class diagram
18-
![alt text](./etc/dirty-flag.png "Dirty Flag")
102+
103+
![Dirty Flag](./etc/dirty-flag.png "Dirty Flag")
19104

20105
## Applicability
21-
Use the Dirty Flag pattern when
22106

23-
* Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead.
107+
* When an operation is resource-intensive and only necessary after certain changes have occurred.
108+
* In scenarios where checking for changes is significantly cheaper than performing the operation itself.
109+
* Within systems where objects maintain state that is expensive to update and the updates are infrequent.
110+
111+
## Known Uses
112+
113+
* Graphic rendering engines to update only parts of the scene that have changed.
114+
* Web applications for partial page rendering or caching strategies.
115+
* Database applications for tracking changes in datasets to minimize write operations.
116+
117+
## Consequences
118+
119+
Benefits:
120+
121+
* Reduces computational and resource overhead by avoiding unnecessary operations.
122+
* Can significantly improve performance in systems where operations are costly and changes are infrequent.
123+
* Simplifies the decision-making process about when to perform certain operations.
124+
125+
Trade-offs:
126+
127+
* Introduces complexity by adding state management responsibility to the system.
128+
* Requires diligent management of the flag to ensure it accurately reflects the state changes, avoiding stale or incorrect data.
129+
* Potentially increases the risk of bugs related to improper flag resetting.
130+
131+
## Related Patterns
132+
133+
* [Observer](https://java-design-patterns.com/patterns/observer/): Can be used in conjunction to notify interested parties when the dirty flag is set or cleared.
134+
* [Memento](https://java-design-patterns.com/patterns/memento/): Useful for storing the previous state of an object, which can work hand in hand with dirty flag logic to revert to clean states.
135+
* [Command](https://java-design-patterns.com/patterns/command/): Commands can set the dirty flag when executed, indicating a change in state that requires attention.
24136

25137
## Credits
26138

139+
* [Game Programming Patterns](https://amzn.to/3PUzbgu)
27140
* [Design Patterns: Dirty Flag](https://www.takeupcode.com/podcast/89-design-patterns-dirty-flag/)
28141
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)

dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@Slf4j
4141
public class DataFetcher {
4242

43-
private final String filename = "world.txt";
43+
private static final String FILENAME = "world.txt";
4444
private long lastFetched;
4545

4646
public DataFetcher() {
@@ -62,14 +62,14 @@ private boolean isDirty(long fileLastModified) {
6262
*/
6363
public List<String> fetch() {
6464
var classLoader = getClass().getClassLoader();
65-
var file = new File(classLoader.getResource(filename).getFile());
65+
var file = new File(classLoader.getResource(FILENAME).getFile());
6666

6767
if (isDirty(file.lastModified())) {
68-
LOGGER.info(filename + " is dirty! Re-fetching file content...");
68+
LOGGER.info(FILENAME + " is dirty! Re-fetching file content...");
6969
try (var br = new BufferedReader(new FileReader(file))) {
7070
return br.lines().collect(Collectors.collectingAndThen(Collectors.toList(), List::copyOf));
7171
} catch (IOException e) {
72-
e.printStackTrace();
72+
LOGGER.error("An error occurred: ", e);
7373
}
7474
}
7575

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
1-
====
2-
The MIT License
3-
Copyright © 2014-2021 Ilkka Seppälä
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in
13-
all copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
THE SOFTWARE.
22-
====
23-
241
UNITED_KINGDOM
252
MALAYSIA
263
UNITED_STATES

dirty-flag/src/test/java/org/dirty/flag/AppTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class AppTest {
3636

3737
/**
3838
* Issue: Add at least one assertion to this test case.
39-
*
4039
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
4140
* throws an exception.
4241
*/

0 commit comments

Comments
 (0)