Search code examples
javadesign-patternsobserver-pattern

Observer Pattern Overload


I'm stuck with the following scenario. It has a code smell, that I'm not happy with, but resolutions to it seems to be as complex, but just in different ways.

I have a scene graph representing business objects. I've designed it so that the business objects themselves are as simple as could be. They're practically POJOs.

Now, 1 entity might be displayed in multiple regions of the scene graph by different nodes.

When the entity changes, all relevant scene graph nodes should change.

I'm hesitant to use the observer pattern on all my entities since I have over 50000 entities on screen at one time.

Since all changes are initiated from the view, right now I'm recursing over the scene graph and forcing a reload of all nodes associated with the changed entity. Doesn't feel right though.

Any suggestions on how this could be done better?


Solution

  • Business Object == Entity? You have 50k of them represented as nodes on the screen, with some entities having more than one node. A user action chages the state of the entity and hence some nodes must be updated. But of course Entities don't know about nodes.

    I would have proxy object wrapping the Entity. He understands the relationship to the Nodes. Updates to the entity go via him, hence he can complete the update and then notify the relevent nodes. Effectively this avoids having to iterate the set of nodes looking for updates.