[Original works] observer patterns in web apps

Source: Internet
Author: User
(Reproduced Please specify: http://zhutty.cnblogs.com, Exchange please Dabigatran: 164858883)

In software engineering, there is an important principle: cohesion-poly low-coupling. This is a measure of the quality of the software design is a standard. The so-called high cohesion, refers to a module within the various elements closely related to the joint completion of a core business. Low coupling, which refers to the loose dependence between the various modules. Creating a low-coupling module, this process also becomes decoupled.

The observer pattern is the low-coupling software design, also known as the Publish (publish)-subscribe (Subscribe) mode. What is the Observer pattern? To give a chestnut: Wang two small in the mountain cattle, suddenly he saw the devil to the village outside, so he put the beacon on the mountain lit. Villagers see smoke signals, all the food of their own hiding up, flower girl saw smoke signals, hurriedly changed into the dress of peasant, the militia put the village of mines on the string, armed with a gun ambush in the village, the county organized villagers retreat, the crow on the tree continue to apply eggs. This is the life of the Observer model, Wang two small in charge of the Beacon Tower, there is no matter, the villagers saw the signal, each made a response. The example use case diagram is as follows:

As can be seen from the use case diagram, each participant is independent of each other and independently of each other, even if they all respond simultaneously to ' smoke signals ' and do not affect each other.

So how do you apply the Observer pattern in a Web project? In the live broadcast system, many information relies on the socket real-time communication, in the traditional practice, after the socket message is pushed back, the message type is judged, then the response business processing method is called. The code is similar to the following: This procedure is responsible for processing the socket message template will depend on all the processing of the socket, A/b ... One of the switch case will accumulate continuously. Each business, such as a deprecated, we delete a file, but also in the socket file corresponding operation, so the degree of coupling is very high. When the business is large, it will bring a certain amount of maintenance costs.

In this case, we can actually use the broadcasting mechanism, the broadcasting mechanism is a manifestation of the observer pattern. When the socket module receives a push message, it sends a broadcast that broadcasts the socket message type and data. Each business module listens to this broadcast message and then completes its own business processing. The Sochet modules and business modules achieve a better understanding of the effects of decoupling, easier maintenance and more stable performance.

[Original works] observer patterns in web apps

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.