A little understanding of the observer pattern, the reactor pattern, the proactor pattern

Source: Internet
Author: User

Recently on the server program IO efficiency this piece to understand the design pattern of the REACOTR mode and Proactor mode, feel with the observer pattern somewhat similar places, the internet also saw some other people on the difference between the understanding of the three, are very careful, in accordance with their own understanding to do a little simple record and summary , if you understand the wrong place, and then slowly in depth and update.

Viewer mode:

It can also be called a publish-subscribe pattern, primarily for multiple objects that depend on the state of an object and, when an object state changes, to notify other dependent objects to make updates. is a 1-to-many relationship. Of course, if the dependent object is only one, it is also a special one-to-ones relationship. In general, the observer pattern is appropriate for message event handling, which notifies event handlers to handle events when they hear an event (a bit like a callback, which is easy to confuse with the callback of the reactor pattern and the proactive mode).

Reactor mode:

The reactor mode, the reactor mode, is an efficient asynchronous IO pattern characterized by callbacks that are processed when the IO is completed and the corresponding function is called. This mode is not really asynchronous, but rather uses the idea of async, when the IO event triggers, notifies the application to make IO processing. The pattern itself does not invoke the system's asynchronous IO functions.

Proactor mode:

The Proactor mode, the pre-camera mode, is also an efficient asynchronous IO pattern, and the feature is a callback, and when the IO event is complete, the corresponding function of the callback handles the completion of the event. This pattern is really asynchronous, is system-level asynchronous, and usually calls the system-provided asynchronous IO functions for IO processing.

The difference between reactor mode and Proactor mode:

The Reacor mode does not call the system asynchronous IO function, which is an imitation asynchronous. The Proactor is a system-level true async that invokes the system-provided asynchronous IO function.

For example, take network IO as an example: when we read data from a socket

1. In the case of reactor mode, the reactor notifies us that "data can be read" and then invokes a callback function that reads data from the socket using the RECV function, similar to the CSocket in MFC, when we rewrite Onrecieve, Internally to call the RECV function to read data from the socket.

2. In the case of Proactor mode, the WSARecv function is called to register the read event, the reactor notifies us that "the data has been read", when the callback function is triggered, the data has been received into the buffer provided beforehand, the entire IO process is done by the operating system, Instead of having to call the RECV function to read the data, we can fetch the data directly in the buffer provided beforehand.

The main differences between the observer pattern and the Recactor mode are the Proactor modes:

The observer pattern, also called the Publish-subscribe pattern, is primarily applicable to a one-to-many dependency between objects, typically used for message distribution and processing. While the reactor mode and the Proactor mode are mainly used for efficient IO mode, the obvious feature is the use of "callback" thinking, which improves efficiency, avoids unnecessary time-consuming waits, and is independent of the dependencies between objects.

A little understanding of the observer pattern, the reactor pattern, the proactor pattern

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.