Can observer pattern be represented by cars and traffic lights?

Posted by eeerahul on Programmers See other posts from Programmers or by eeerahul
Published on 2012-05-28T05:12:37Z Indexed on 2012/05/30 16:59 UTC
Read the original article Hit count: 224

Filed under:

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?

© Programmers or respective owner

Related posts about design-patterns