[JAVA] use and analysis of Observer mode in Design Mode

Source: Internet
Author: User

Address: http://blog.chinaunix.net/uid-29140694-id-4123507.html

The observer pattern is a behavior design pattern that is important and commonly used in 23 design patterns. The observer mode defines a one-to-many dependency, allowing one or more observer objects to monitor a topic object. In this way, when the status of a topic object changes, all observer objects dependent on this object can be notified so that these observer objects can be automatically updated.
The structure of the observer mode is as follows:

It contains four roles:
(1) Abstract topic role (Subject): Abstract topic provides an interface, including adding, removing, and notifying the observer.
(2) ConcreteSubject: a topic is the object observed by the observer. It contains a list of all the observers of the topic and is the concrete implementation of the abstract topic. When the topic status changes, the observer is notified.
(3) Watcher: it is the abstract interface of a specific observer. It contains the method for updating the observer.
(4) ConcreteWatcher: implements the abstract observer interface and provides methods to update itself.
The following describes the Code Implementation of the observer mode:

1. First file: Subject. java

/*** Abstract topic role */public interface Subject {/*** add Observer * @ param watcher */public void addWatcher (Watcher watcher ); /*** remove the Observer * @ param watcher */public void removeWatcher (Watcher watcher ); /*** notify the Observer * @ param str */public void notifyWatcher (String str );}

2. Second file: Watcher. java

/*** Abstract Observer */public interface Watcher {/*** observer update * @ param str */public void update (String str );}

3. Third file: ConcreteSubject. java

Import java. util. arrayList; import java. util. list; /*** specific topic role */public class ConcreteSubject implements Subject {/*** observer List of the topic */private list <Watcher> List = new ArrayList <Watcher> (); @ Override public void addWatcher (Watcher watcher) {// TODO Auto-generated method stub list. add (watcher) ;}@ Override public void removeWatcher (Watcher watcher) {// TODO Auto-generated method stub list. remove (watcher) ;}@ Override public void policywatcher (String str) {// TODO Auto-generated method stub for (Watcher watcher: list) {watcher. update (str );}}}

4. Fourth file: ConcreteWatcher. java

/*** Specific observer role */public class ConcreteWatcher implements Watcher {@ Override public void update (String str) {// TODO Auto-generated method stub System. out. println (str );}}

5. Fifth file: TestMain. java

/*** Test the Main method */public class TestMain {public static void main (String [] args) {Subject sbj = new ConcreteSubject (); Watcher watcher1 = new ConcreteWatcher (); watcher watcher2 = new ConcreteWatcher (); Watcher watcher3 = new ConcreteWatcher (); Watcher watcher4 = new ConcreteWatcher (); sbj. addWatcher (watcher1); sbj. addWatcher (watcher2); sbj. addWatcher (watcher3); sbj. addWatcher (watcher4); sbj. notify Watcher ("hello world! ");}}

As shown in the code above, the use of the observer mode is relatively simple and clear. The process of its use is to first abstract the observer and the observer, and then implement the observer and the observer, and the observer contains the method for notifying the observer when the subject changes.
The observer mode has the following advantages:
(1) It separates the observer from the observer and associates the relationship between the observer and the observer through the abstract observer and the abstract observer. When one party changes, the execution of the other party is not affected, this reduces program coupling.
(2) It can support different implementations of specific observers and facilitate program expansion.
(3) The number of observers is variable. The topic can dynamically add or remove observer objects.
(4) When the status of the observer changes, the observer is notified automatically. If not, then, the observer needs to monitor whether the status of the observer changes by means of scheduled tasks. The way that the observer actively notifies is higher than the way that the observer regularly monitors the status.
Disadvantages of observer mode:
The observer mode is a common trigger mechanism, which forms a trigger chain and processes the methods of each observer in sequence. But at the same time, this is also a disadvantage of the observer mode. Because it is a chain trigger, when there are many observers, the performance problem is quite worrying. In addition, in the chain structure, it is easy to see the error of loop reference, resulting in system false death.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.