Learning C # design patterns series notes (2) Observer patterns,

Source: Internet
Author: User
Tags notification center

Learning C # design patterns series notes (2) Observer patterns,

I. References

1. Head First Design Patterns (Chinese name: deep dive into Design Patterns)

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

Ii. Observer Mode

  • Basic knowledge

Provides an observer (Database, notification center, etc.). Multiple observers register on the observer. When the data changes that the observer cares about, the observer notifies the observer. The observer can actively send the data to the observer (push mode) or the observer can obtain the data (PULL mode) by the observer after receiving the notification ).

  • Example

Nowadays, users can subscribe to various kinds of weekly magazines through their email addresses, and the publishing society responsible for these weekly magazines regularly sends the weekly magazines to users' mailboxes. These weekly magazines are divided into two types, one is toutiao.com (specifically recommended by the publishing house to users with detailed descriptions) and the other is a link group (that is, the contents with summaries and hyperlinks, if you are interested, you can click the hyperlink to learn more ). If you want to obtain weekly magazines of a specified publishing house every week, you must first register them in the publishing house. If you want to subscribe to weekly magazines of multiple publishing houses, you must register them in each publishing house. Of course, users have the right to cancel the subscription.

Here, the User is the Observer, the Press is the Observer Observable, the User subscribes to a Press weekly is the registration register, the User cancels the subscription is remove, and the periodical delivery of weekly is the notification notify, toutiao is the push method, that is, when Press actively pushes data to the User, the link group is the pull method pull, that is, after receiving the notification, the User actively obtains data from Press. Therefore, Press has the registerObserver function (add observer), removeObserver function (remove observer), updateImportantContent function ("Push method" pushes headlines), updateHyperlink function ("Pull Mode" Send link), the User has the notifyPush function ("Push mode" gets the headlines, and each User has different processing methods for headlines) notifyPull function ("Pull Mode" to obtain the link group, each User has different processing methods for the link group ). The User can call the registerObserver function of Press to subscribe to weekly, and call the removeObserver function to unsubscribe to weekly. UML.

As shown in, to ensure the encapsulation of Press, specifically, the updateImportantContent function and updateHyperlink function are private to ensure that Press has the right to send notifications (if no encapsulation is ensured, assume that an unhealthy author wants to exclusively occupy his work in the weekly report, so he may call the updateImportantContent function and updateHyperlink function to achieve his goal, the coupling is too high, so I think: For C #, we recommend that you use the event keyword to implement the observer mode. The specific implementation is shown below.

  • When can I consider using the observer mode?

When an object depends on the data of another object, or the main thread is notified after the asynchronous operation of the sub-thread is completed, the observer mode can be considered.

  • Specific C # implementation

The code for the design pattern (C #) is all put on git: https://github.com/MGKING3/DesignPatternsUseCSharp

I use VS2015, which is a whole project. It can be downloaded and updated from time to time.

If you can not use git, Baidu cloud can also download, address: http://pan.baidu.com/s/1bp7Txuf

Hope to learn from each other. Thank you.

  • Related OO principles

1. encapsulation principles

2. Multi-Purpose Combination (has-a) and less inheritance (is-)

3. Try to "interface-oriented"

4. The pursuit of "loose coupling"

Related Article

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.