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

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top