Interaction between objects-Observer mode (6)

Source: Internet
Author: User
22.6 observer mode and MVC

The observer mode is also applied to the Model-View-controller architecture. MVC is an architecture mode that includes three roles: model ), view and controller ). The model corresponds to the observation target in the Observer mode, while the view corresponds to the observer. The controller can act as the intermediary between the two. When the data at the model layer changes, the view layer automatically changes its display content. 22-7:

Figure 22-7 MVC Structure

In Figure 22-7,The data provided by the model layer is the object observed at the view layer.The view layer contains two chart objects used to display data. One is a column chart and the other is a pie chart. The same data has different chart display methods, if the data at the model layer changes, the two chart objects change accordingly. This means that the chart objects depend on the Data Objects provided by the model layer, therefore, any changes to the status of data objects should be immediately notified. At the same time, the two charts are independent of each other and there is no association between them, and there is no limit on the number of chart objects. You can add new chart objects, such as line charts, as needed. When adding new chart objects, you do not need to modify the original class library to meet the "Open and Close principle ".

Extension

You can refer to the relevant materials for in-depth study of the MVC model, such as the technical documentation provided by Oracle 《Java SE application design with MVCFor more information, see http://www.oracle.com/technetwork/articles/javase/index-142890.html.

22.7 observer mode Summary

The observer mode is a very frequently used design mode. for mobile applications, web applications, or desktop applications, the observer mode is almost everywhere, it provides a complete solution for implementing the interaction between objects. The observer mode can be used in any scenario involving one-to-one or one-to-many object interactions. The observer mode is widely used in Gui event processing in various programming languages. It also uses the observer mode in event-based XML parsing technology (such as sax2) and Web event processing.

1. Main advantages

The observer mode has the following advantages:

(1)
The observer mode separates the presentation layer from the data logic layer, defines a stable message update transfer mechanism, and abstracts the update interface so that different presentation layers can act as specific observer roles.

(2)
The observer mode establishes an abstract coupling between the observer and the observer. To observe a target, you only need to maintain a set of abstract observers without understanding the specific observer. Because the observation targets are not closely coupled with the observer, they can belong to different abstract layers.

(3)
The observer mode supports broadcast communication. The observer object will send notifications to all registered observer objects, which simplifies the design of one or more systems.

(4)
The observer mode meets the requirements of the "open and closed principle". Adding new observers does not need to modify the original system code. When there is no association between the observer and the observed object, it is also convenient to add new observation targets.

2. Main disadvantages

The main disadvantages of the observer mode are as follows:

(1)
If there are many direct and indirect observers for an object to be observed, it takes a lot of time to notify all the observers.

(2)
If there is a circular dependency between the observer and the observation target, the observation target triggers a circular call between them, which may cause the system to crash.

(3)
The observer mode does not have a mechanism to let the observer know how the observed target object changes, but only knows that the observed target has changed.

3. Applicable scenarios

You can consider using the observer mode in the following situations:

(1)
An abstract model has two aspects, one of which depends on the other. These two aspects are encapsulated in independent objects so that they can be changed and reused independently.

(2)
An object changes one or more other objects, but does not know how many objects will change or who these objects are.

(3)
You need to create a trigger chain in the system. The behavior of object A will affect object B, and the behavior of object B will affect Object C ......, You can create a chain trigger mechanism in observer mode.

Exercise

To develop a real-time online stock software, sunny software must provide the following functions: when the price of a stock purchased by a stock purchaser changes by 5%, the system will automatically send a notification (including new prices) to all investors who have bought the stock. Design and implement the system in observer mode.

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.