Learn C # Design Patterns series notes (2) Observer pattern

Source: Internet
Author: User
Tags notification center

First, the reference note

1. "Head First design Patterns" (Chinese name "Simplified design mode")

2. Wikipedia, observer mode, Https://zh.wikipedia.org/wiki/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F

3.msdn,event (C # Reference), https://msdn.microsoft.com/zh-cn/library/8627sbea.aspx

Second, the Observer pattern

    • Basic knowledge

An observer (database, Notification center, etc.) is provided, and multiple observers are registered with the observer, and the observer notifies each observer when the data that the observer cares about changes. The observer can actively pass the data to the Observer (push mode) or the observer will take the data (pull mode) to the observer after receiving the notification.

    • Specific examples

Today, users can subscribe to a wide range of weekly magazine, and responsible for these weekly publishing society regularly sent weekly to the user's mailbox, these weekly pages are divided into two kinds, one is the headlines (that is, the publishers are particularly recommended to the user, there is a detailed description of the content), a link group (is some summary, Hyperlinks to the content, if the user is interested, you can click the hyperlink to learn more. If a user wants to get weekly weekly for a designated publisher, they need to register on the publisher first, and if they want to subscribe to multiple publishers ' weekly, they need to register on each publisher. Of course, users have the right to unsubscribe.

Here, the user is the Observer observer, Publisher Press is the Observer Observable,user subscribe to a newspaper magazine is registered Register,user cancel the subscription is to write off remove, Scheduled to send weekly is to inform notify, and the headline is push mode push, that is, the press initiative to push data to user situation, link Group is pull, that is, after receiving notification, user actively to press to obtain data. Therefore, the press has the REGISTEROBSERVER function (add observer), Removeobserver function (remove observer), Updateimportantcontent function (push mode pushes the headline), Updatehyperlink function ("Pull mode" send link), user has the Notifypush function ("Push way" to get the headlines, each user to the headlines are not the same way), Notifypull function ("Pull way" to get the link group, Each user handles the link group differently). User can call the press's Registerobserver function subscription weekly, call the Removeobserver function to unsubscribe from weekly. Uml.

As shown, in order to guarantee the packaging of the press, specifically refers to the Updateimportantcontent function and the Updatehyperlink function private, to ensure that the press has the right to give notice (if not to ensure encapsulation, suppose there is a bad author, Just want to let his work exclusive entire weekly, then he completely may call updateimportantcontent function and Updatehyperlink function to achieve their own purpose, but so, coupling degree is too high, so I think: for C #, recommended use The event keyword implements the Observer pattern, which is shown below.

    • When to consider using observer mode

Consider using observer mode when an object relies on another object's data changes, or when a child thread notifies the main thread when the asynchronous operation is complete.

    • Specific C # implementations

Related code for design mode (C #) I put it on git: https://github.com/MGKING3/DesignPatternsUseCSharp

I use VS2015, is the whole project, download can be used, from time to date.

If you can't use Git, Baidu Cloud can also be downloaded, address: Http://pan.baidu.com/s/1bp7Txuf

Hope to learn from each other, thank you

    • Related OO Principles

1. Packaging principles

2. Multi-use combination (has-a), less inheritance (is-a)

3. Try to "interface-oriented"

4. Pursuit of "loose coupling"

Learn C # Design Patterns series notes (2) Observer 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.