7 Replies Latest reply: May 11, 2010 1:28 PM by PhHein RSS

    Observer pattern vs Property change listener

    843851
      A observer can observer the changes of the model. so do property change listener.

      so, anyone can explain what's the difference between them?
        • 1. Re: Observer pattern vs Property change listener
          843851
          I think you have got some misunderstanding for property change listener. As per java docs, it is intended for beans. Whenever a bean changes its "bound" property, its propertyChange event gets fired.

          • 2. Re: Observer pattern vs Property change listener
            843851
            yes, you are right
            so that, we can adapt observer pattern by the usage of property change listener as well ?

            • 3. Re: Observer pattern vs Property change listener
              843851
              A observer can observer the changes of the model. so do property change listener.
              "Observer" is a design pattern in which an "Observer" is notified when changes occur to a "subject".
              Beans with property change listeners are one example of a usage of this pattern.
              Here the bean is the "subject" and each registered PropertyChangeListener is an "observer".


              • 4. Re: Observer pattern vs Property change listener
                843851
                The difference between the propertyChangeListener and the Observer - Observable pattern is one of instantiation. The propertyChangeListner is an implementation of the observer pattern.
                The observer - observable pattern is an abstract description of a problem - solution pair, the propertyChangeListner and eventListners etc. are all concrete implementations of this pattern.

                - hope this helps.
                • 5. Re: Observer pattern vs Property change listener
                  843851
                  Thanks , I always doubt that.
                  so, now i can confirm property change listener is "one" of the implementation of Observer pattern.
                  • 6. Re: Observer pattern vs Property change listener
                    843853
                    @aragon28 property change listener IS a form of observer but a 'loose' implementation. I never liked the implementation because setting up listeners and state change callbacks are based on text strings. Java makes even a worse implementation of this by not making the properties some kind constant, so for you to know what is happening and when is happening, troubleshooting can be a nightmare.
                    However, the other implementation of the pattern is interface based, such as the one you see in ActionListener and others in the awt, swing event/notification model. It is in my opinion a true observer pattern because looking at the subject(and in some cases the observer), you can tell what kind of events are supposed to happen and you can trace back easily when something is happening and from where it is getting called.

                    My last thought: I HATE PROPERTY CHANGE. Use the ones provided by frameworks you may need to use, but never use them for your own implementation of the pattern.
                    • 7. Re: Observer pattern vs Property change listener
                      PhHein
                      Please don't post in threads that are long dead and don't hijack other threads. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

                      I'm locking this thread now.