Pattern for side effects 
Author Message
 Pattern for side effects

Suppose I have a number of objects (of different classes) that
containt each other and form some sort of graph

         B   C
       D<-     E                                    

There is only one A but all others can come manyfolg (D1..Dn).

Now for some reason an update (eg. amount change) of D should cause a
change in E (balance change). Furthermore D is a model for a
DView/Controller (a widget).

Objects of D class do not need to be in such a tree but can also exist
alone, in which case such a side effect if of course not required.

How would I implement this ? I came up with the following ideas:

(1) tell Ds about the existance of E and how to find it. Let D cause
    the side effect itself. This is not a very good example of
    encapsulation I believe.

(2) Let the view cause the side effect. This has the same problems as
    (1) The DView should know about D only.

(3) Let D issue a #changed message. This would require B to propagate
    the change to A. Again: B knows more than it should

(4) Register E as a dependent of D when the tree is built. This is
    okay as long as the nature of the side effect does not depend on
    A,B, or C.

(5) Install a common handler for these kind of side effects and plug
    it into D and E (and possibly A, B and C). The common handler
    could even be A itself, becuase A is easy to find for all 5

(6) Let A (or another master) control the entire operation,
    i.e. change D and do the side effect in E. This would have the
    benefit that the sequence of operations will be controllable. In
    changed/update chains this is not always easy. But alas, the DView
    can no longer be used. I would need a view on the master
    object. But there can be lots of Ds.

This post turned out a bit confusing (and thus properly reflects my
state on mind).

Thu, 23 Dec 2004 03:59:59 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Dialogs and side effects

2. Skipping print preview side effect ArnĂ³r

3. Avoiding functions with side effects

4. INI file side effects

5. Functions without side effects (was Old confusion)

6. Wrapping a side-effecting function library

7. Side-effect free functions

8. Forth's popularity, side-effect

9. local variables don't cause side effects

10. One advantage of computing by side-effect

11. Side effects in pure functional languages

12. FP and side effect intensive programs


Powered by phpBB® Forum Software