Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways to implement 'dirty'-flag functionality [closed]

Almost every programmer did it once in his life: setting some flag if a variable's value changed. There's always lots of properties and you want to keep track if something changed

  1. in any property
  2. in a specific property
  3. or in some set of properties

I'm interested in different ways to implement the "dirty-flag" functionality for the above situations, besides the standard object wide dirty flag being updated on each property change. There must be something better than putting "dirty = true" in each setter: it just looks ugly and is a tedious work.

like image 650
MicSim Avatar asked Feb 16 '09 16:02

MicSim


People also ask

Which class allows for dirty checking by setting a dirty flag?

Class ExecutionContext. Object representing a context for an ItemStream . It is a thin wrapper for a map that allows optionally for type safety on reads. It also allows for dirty checking by setting a 'dirty' flag whenever any put is called.

What is dirty flag pattern?

The Pattern A “dirty” flag tracks when the derived data is out of sync with the primary data. It is set when the primary data changes. If the flag is set when the derived data is needed, then it is reprocessed and the flag is cleared. Otherwise, the previous cached derived data is used.


1 Answers

For my DAO I keep a copy of the original values as retrieved from the database. When I send it to be updated, I simply compare the original values with the current. It costs a little in processing but it is a lot better than having a dirty flag per property.

EDIT to further justify not having a dirty flag: if the property returns to its original value, there is no way to reflect that, the dirty flag continues dirty because the original value was lost.

like image 155
Otávio Décio Avatar answered Oct 05 '22 18:10

Otávio Décio