本文介绍了实现“脏"标志功能的不同方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎每个程序员一生都做过一次:如果变量的值发生变化,则设置一些标志.总是有很多属性,如果有什么变化,你想跟踪

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. 在任何财产
  2. 在特定属性中
  3. 或某些属性集

我对针对上述情况实现脏标志"功能的不同方式很感兴趣,除了在每次属性更改时更新标准对象范围的脏标志之外.肯定有比在每个 setter 中放置 "dirty = true" 更好的东西:它看起来很丑,而且是一项乏味的工作.

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.

推荐答案

对于我的 DAO,我保留了从数据库中检索到的原始值的副本.当我发送它进行更新时,我只是将原始值与当前值进行比较.它的处理成本有点高,但比为每个属性设置一个脏标志要好得多.

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.

这篇关于实现“脏"标志功能的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 18:19