I've got an object with a complex hierarchy, something like:
Document Paragraphs Paragraph Words Word
and I need to implement a dirty flag on the
Document object. In general, there can be a huge number of Paragraphs and Words, and paragraphs and words have many properties that can be changed. So I'm having trouble figuring out where to capture state change so I know when the document is dirty. I could:
- Give every single Paragraph a reference back to the Document object, and give every single Word a reference to its containing Paragraph, and then in every property change, walk up the hierarchy and tell the document that it's dirty. But that's a lot of references and hooks on property changes.
- Find any place in the application that changes any paragraph or word property, and have that code also tell the document that it's dirty. But then it would be hard to know that I had found all such places, and I'd have to make sure that any future document changes also made sure to do this, which seems hard to maintain.
- Record the initial state and then test the current state against the initial state. But since the number of objects is very large and the hierarchy is somewhat deep and complex, that seems like it would be slow and memory intensive.
Is there a better design pattern for this situation?