The observer pattern and nested objects
I'm just struggling a little with the observer pattern and preventing observers from registering multiple times.
I've got a
model
that implements an Observable interface, so registerObserver
/removeObserver
and updateObserver
functions, and a View
with a constructor that calls this.model.registerObserver(this)
.
The problem is each view can have nested views, and those views are created with the new
keyword. That means (I think) that I'm unable to run a check in the registerObserver
function to make sure that the view isn't already registered :/ Am a bit stuck!
For example:
The Model
The View
Concrete implementation of the View above that involves nested Views (only looking at onRender
)
FormView (extends View):
No doubt this is basic, but I'm stuck! 😆
**After many shit ideas, chatGPT has suggested each View is assigned a random id, which I guess would work, but there must be a better solution0 Replies