Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I wanted to verify with all of you, if I have a correct Observer Pattern analogy. The scenario is as follows: Consider, at a junction, there is a traffic signal, having red, yellow and green lights respectively. There are vehicles facing the traffic signal post. When it shows red, the vehicles stop, when it shows green, the vehicles move on. In case, it is yellow, the driver must decide whether to go or to stop, depending on whether he/she has crossed the stop line or not. At the same time, there are vehicles that do not care about the signal. They would do as they like.

The similarities are that, the Traffic Signal happens to be the subject, notifying its states by glowing the appropriate lights. Those looking at it and following the signal are the ones subscribed to it, and behave according to the state of the subject. Those who do not care about it, are sort-of un-subscribed from the traffic signal.

Please tell me, if you think this is a correct analogy or not?

share|improve this question

migrated from stackoverflow.com May 30 '12 at 1:55

This question came from our site for professional and enthusiast programmers.

1  
You can make observers that are aware of the state change, but still don't apply (e.g. for your story, an ambulance\police car would be a good analogy). This is still 100% observer pattern. –  Kahil May 30 '12 at 4:56

5 Answers 5

up vote 3 down vote accepted

The analogy will work, but you added more complication such as what to do at yellow light.

A simpler way is to only have green and red light; the traffic light would indicate two state changes:

  • red to green: traffic can go.
  • green to red: traffic must stop.

Note that actions that should take place as a result of this state change does not concern the observed subject (i.e. the traffic light doesn't make a car stop).

Without the subscription, interested parties would have to continuously poll the traffic light state. Therefore, those 'who are not interested' can be left out of the analogy as they're not important.

share|improve this answer
3  
I dont see any problems with the yellow light. The Observers are notified when it turns yellow, and they apply their logic accordingly. The Subject shouldnt apply any logic, just send the signals to those that are observing. –  Brady May 28 '12 at 5:40
    
I agree: In case the signal shows yellow, there is additional logic to be implemented. –  eeerahul May 28 '12 at 5:47
    
@Brady there's no problem with it, but for the sake of analogy, the less complex it is while still covering the pattern the better. –  Jack May 28 '12 at 5:56
1  
@Jack, agreed, but I think its necessary to point out that any logic to be applied for any signal should be implemented in the Observers, not in the subject. –  Brady May 28 '12 at 5:58
3  
@MartinSpamer One could imagine a device inside the car that would alert the driver of the state change. Besides, the analogy came from the OP, so instead of down voting my answer I would suggest adding your own. –  Jack May 28 '12 at 12:41

I think your analogy is wrong, depending on how technically correct you want the analogy to be.

Although the car "observes" the traffic light, the observer pattern is about the subject actively notifies its observers about a change in state.

If the traffic light analogy should apply, then the traffic light should actively send out a message directed at each car approaching the traffic light when it changes. That is a bit off IMHO.

share|improve this answer

I think the analogy is not the best one. When you drive a car you really don't care about EVERY traffic lights state changes, you only care about the CURRENT state of the next traffic light you will encounter. A better one, in my opinion, would be economic tv channels and Stock Exchange. Stock Exchange would be your subject and tv channels your observers. They actually NEED to be noticed at every state change of your subject, in real time.

share|improve this answer

I don't think this is a right analogy. Observers register themselves with the Subject initially. In the traffic example, this would get a bit complex because cars would need to register/unregister themselves at every traffic light based on whether the signal is green or not. For me, an appropriate analogy would be multiple threads doing their job and checking their interrupt status periodically.

share|improve this answer
    
I had put only one traffic signal post in the scenario. Hence, registering/unregistering at every traffic light is ruled out. And, I also said looking at the Traffic Light is a symbolic way of registering to it. If the driver is not looking at it, he is un- registered. –  eeerahul May 29 '12 at 5:43

The main problem with the observer pattern and a traffic light example, as I see it, is that the observer pattern is for notifying subscribers of state changes. Theoretically, an observer (a car in traffic) is concerned with the current state of the traffic light, not as much about when it changes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.