C # Support for Observer (OBSERVER) mode (i)

Source: Internet
Author: User
First let's look at what is the Observer pattern, the observer pattern is the behavior pattern of the object, also called the dependent/dependent mode, publish-subscribe mode, source-listening mode. Observer pattern: Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated (GOF).

If we have a mail system in our project, we may need to send SMS and RTX message recipients when sending the email, then we can design it with the observer mode, and if we need to add a third method of notification later, our system will easily add the new notification mode. The Observer pattern is also widely used in C #, and the most common is the event monitoring mechanism we use for various controls.

There are four types of participants in the implementation of the Observer pattern, respectively:

1. Abstract Theme/goal (Subject)

It can have as many observers as possible, save a reference to the observer in a collection object, and provide an interface for adding and removing observer objects, typically implemented with an abstract class and interface.

2. Abstract Observer (Observer)

An update interface defined for all specific observers that notifies the specific observer to update itself when the subject/target changes. Typically implemented with an abstract class or interface.

3. Specific theme/objective (ConcreteSubject)

Stores the state of the specific observer object and notifies all registered observers when its state has changed. The specific subject/goal is also known as the Observer.

4. Specific observers (Concreteobserver)

Maintains a reference to a specific subject/target and stores the relevant state that is consistent with the specific subject/target, and implements a specific update interface for the abstract observer to align its state with the subject/target state.

We use the Observer mode to implement a Simple mail sending system, and send the message and send SMS and RTX messages to notify the email recipient.

The above is the C # on the Observer (Observer) mode of support (a) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.