Question

I am playing with some design patterns and i am able to add and remove Observer Drone object from an Observable Borg objects list of observers. I want to print an attribute of each of the Observable objects observers. Such as the the Observers Object ID attribute.

A bit more explanation:

I have a Borg class which extends Observable, I have a Drone class which implements Observer.

Each drone has an ID attribute, in my program i have a list of drones and they are all registered as observers of my Borg object.

I also have a list of Drones, which are all registered at first as Observers, i then remove some of these observers from the borgs observer list.

I want to itterate through the Drone list and test if the drone is an observer of the observable borg object

Was it helpful?

Solution

If you are referring to java.util.Observable, there's no clean way to do it. The class has a private Vector field to store the registered Observable instances. Sub classes do not have access to it.

I recommend you implement your own Observer/Observable pattern.


By not clean, I mean you could use reflection, but don't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top