The Observer Pattern of Java Design Patterns

 

           A large number of event listeners are used in android, which is essentially the observer mode. So prepare a brief summary and review, and write it down to deepen your memory.

         1 Overview

           There are two roles in the Observer pattern: Observer and Observed, both of which have their abstract interfaces respectively. The observer can listen to multiple observers at the same time. When the observer changes, it notifies all the observer objects so that they can obtain the corresponding information.

         2. Advantages

          The observer pattern is a good implementation of the principle of high cohesion and low coupling, and a low-coupling relationship is established through the interface (there is still a dependency between the two interfaces). The observer does not need to obtain update information by itself, and the observer will actively notify the observer when there is an update. The person being observed does not care who the information is sent to, as long as you monitor it, it will be given to you.

         3. Class structure diagram

             

                 (Figure transferred from: http://www.blogjava.net/supercrsky/articles/202544.html )

 

         4. Code

              The code is saved, and it should be well understood by looking at the above class structure diagram.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361663&siteId=291194637