I have a program with a number of variables A, B, C..., and they have dependency relationship. For example, C's value depends on A and B's values. Whenever the value of a variable is changed, I need to update all its downstream variables. For example, if A's value is changed, I will need to recompute C's value. If C's value is changed, too, I will need to recompute the values of those variables dependent on C. Now I have code like this:
A a;
B b;
C c;
// ...
void updateC()
{
C newC = calculateC(a, b);
if (c != newC)
{
c = newC;
updateD();
updateE();
}
}
As the number of variables grows this code is too difficult to maintain. There are also complex logic such as if updateD is successful then you do not need to call updateE. Is there a standard design pattern or library for such problems?
Aucun commentaire:
Enregistrer un commentaire