Java Design Pattern 2-viewer mode

Source: Internet
Author: User

What is the Observer pattern?

What is the Observer pattern? The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and updated automatically.

Here, the object of change is called the observation target, and the object being notified is called the Observer. An observation target can correspond to multiple observers, and these observers are not interconnected, so you can add and remove observers as needed, making the system easier to scale.

Observer mode, also known as publish-subscribe mode

What are the advantages of the Observer pattern?

1, it's a good solution to the problem of coupling in a one-to-many dependency, so that when the state of an object changes, all objects that depend on it are notified and refreshed automatically.

2, when you want to add a dependent object, do not change one end of the code.

When do you use Observer mode?

1, this mode is usually used to implement the event processing system. (You sent a dynamic dynamic to the QQ to be sent to the person can see, add Group is registered, back Group is notify)

2, there is a better understanding, when one thing is done, you can do other things.

such as: With Money can buy food, wear, drink. (One-to-many). When you spend money in the bank will be, SMS notify you, email notify you, notify you, you can also cancel SMS notification.

What are the points of note when using Observer mode?

If there is a cyclic dependency between the observers, the observed will trigger a cyclic call between them, causing the system to crash. It is important to pay special attention to this when using the Observer pattern.

How to design the observer pattern, how to do?

A very good understanding of a blog

http://blog.csdn.net/chenssy/article/details/8955696

Java built-in observation mode

Observable: is a class used when subclasses.

1, this class to implement the registration, notification, removal of three methods

2, notifications are available in two ways (two methods notifyobservers () and notifyobservers(Object Arg))

3, call the setchanged() before calling the two notification method;

Observer: There is an Update method inside the interface

Java Design Pattern 2-viewer mode

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.