How to avoid infinite loop in Observer pattern?
design-patterns, java
Solution
What you are looking for is a graph-traversal algorithm that detects cycles. One simple approach (that only works in a single threaded scenario) is to keep a global/static counter to let each top-level `update()` invocation get a unique identifier. Each observer then tracks whether already it has processed the update with the given identifier (ID) and in that case ignores it. This means that your `update` method will have to be expanded with a parameter with the ID number of the specific update.
Problem
I have only one class with many instances. Every instance is observer of couple of other instances. As well every instance can be observable by couple of another instances. How to avoid infinite loop of calling update() in observers?