# Observer Pattern??

Ty Karl
Greenhorn
Posts: 22
Hi guys,

This is my problem -- a fundamental understanding of How observers work.

I have a class (Eventsource) and 2 other classes(A + B) that must watch when Eventsource does something.

A is dependant on B ( B must be True before A can start working) , My event source I think should have a boolean while set to false, then a loop to notify A + B. I need to keep doing this Until my boolean is true.
if this is possible how do I send a bool value from the observer to the event source telling it that it is now true so i can stop the while (again if this is the correct way)
Must A also be an observer of B?

I have read some of the posts and one says that my evnts should be a class themselves( the telephone eg)

Rob Spoor
Sheriff
Posts: 20665
65
Tyrone Toplis wrote:Must A also be an observer of B?

If I understood your problem, then yes. Eventsource shouldn't have to know who its observers are, and B shouldn't have to know who is interested in its boolean value. A should be the only thing that ties them together, by being an observer of both others.

K. Tsang
Bartender
Posts: 3521
16
Hello why don't we start with 2 classes: one observer and the other observee. Say we have a table of data and the table has a table model. Now you want to do something to the table so that the table model immediately reflect this change. So which is the observer?

Now extend this to 3 classes. A observes B and B observes C meaning A observes C. Transitive property from math. So your 2 other classes one of them needs to be observer of the other.

Hope this makes sense. Ideally observer pattern is similar to AJAX on the web.

Ty Karl
Greenhorn
Posts: 22
Hi Rob and K,

thanks for the help, it has made an understanding of the problem with regard to observers being observerable as well, with regard to that Ill try it and show my code for what ive done.

Another question: When I refered to sending a true state back to the event source.. Maybe this will give you a better understanding.

Sun = Event source ( on for 12 counts/ off for 12)
Flower = observer (must open when sun is up/ close when dusk)
Bird = observer( also wakes when sun up/ sleeps @ dusk ....& feeds off flower nectar)

there are 10 units of nectar per flower and 10 flowers, bird feed on one for one count (hourchange()) then moves to next on so on.
FLOWER & BIRD SLEEP onDayend and Wake onDayStart

I imagine Rob that my Flower is observer and observerable since the bird can only feed when flower is open (But saying that when sun is up BOTH are functional.. so im not 100% sure about the bird being dependant on flower?)

Does an event source fire once? like in this case my eventsource be in a loop till all the nectar is finished (ie: 100 units of nectar / 12 hours = 8 days and 4 hours plus the sleep time of 8 days TOTAL = 196. this seems somehow seems strange to me, the sun should go up and down without being concerned with how much nectar is left etc.. like you said earlier. so im understanding then confusing myself again.

Rob Spoor
Sheriff
Posts: 20665
65
In this case, the sun should only fire two events (or maybe four):
- the sun has started to go up (optional)
- the sun is up
- the sun has started to go down (optional)
- the sun is down

The flower also has similar events: only for the transitions.

It is now up to the bird to listen to events from both, and when both the sun is up and the flower is open, it can just keep coming by regularly until a) the flower is closed, b) the sun is down, or c) the flower is empty. So you see, the interval is handled by the bird, until it is signaled with an event to stop (or pause) this interval.

I imagine Rob that my Flower is observer and observerable since the bird can only feed when flower is open (But saying that when sun is up BOTH are functional.. so im not 100% sure about the bird being dependant on flower?)

If you would break one of the dependencies, I would break that between the sun and the bird. After all, the bird is not really interested in the sun but in the flower.

However, there are also flowers that only open during the night. If the bird would listen only for sun events, it would go to the flower only to find it is closed. If the bird would listen only for flower events, it would get notified during the night - when it should sleep. So being notified by both sources it needs is quite reasonable.

Ty Karl
Greenhorn
Posts: 22
Hi Rob,

thanks for that, it has added more clarity, Ill begin to write the code for it and ask for help when need be.
Til then cheers and thanks for the help

Tyrone