Where to subscribe to events of the inner object?

后端 未结 3 1528
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 12:35

I\'m often encountering a situation where I must decide where to subscribe to events of the inner object?

For example, I have an object model looks like this:

3条回答
  •  一生所求
    2021-01-14 13:16

    There is way too much public stuff in your example. Hope I'll make sense below:

    1. ClassB contains an object of type ClassA, and handles some ClassA events
    2. ClassC contains an object of type ClassB but events are ignored.
    3. ClassD contains an object of type ClassC and handles events from the ClassA object inside the ClassB object contains in this ClassC objects

    #2 and #3 are not good: ClassC should handle and implement the events, handling them and letting them "bubble up" (invoking their own, same, event) for ClassD to handle correctly.

    Basically, all of them should handle all events, either reacting to them (as in ClassB to ClassA's events) or just propagating them.

提交回复
热议问题