Talk about the design mode ~ Observer mode (observer)

Source: Internet
Author: User

Observer mode, also known as Publish/subscribe mode (publish/subscribe) and monitor mode. In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes. This is usually done by calling the methods provided by various observers ..

When can it be used?

This mode is usually used to implement the event processing system. For example, when a data warehouse operation adds a function, when the addition is triggered, therefore, code blocks that subscribe to this add event request will be triggered and executed. In this process, the add operation of data warehouse is the observer, and the subscriber is its observer, after an observer is initiated in a certain situation, the observer's code segment will be executed.

For an email sending function, all modules that subscribe to this function are triggered as long as the system reaches a certain point in time, all code modules that subscribe to this function will be executed. In this case, we have seen many of them, such as news subscriptions and preferential subscriptions for many websites. For the underlying developers, If You Want To dynamically record logs after an operation error occurs in your curd operations, you can also use this mode to achieve it, you only need to subscribe to log recorded events on the web layer or BLL layer.

Structure of observer Mode

Observer mode implementation

Subjectbase: base class of the observer. It includes the function of adding, deleting, and triggering the observer.

Subject: One of the observer

Iobserver: interface of the object to be observed, so it must be implemented by the observer. subjectbase also receives an iobserver object to trigger all observers.

Observer1: observer 1

Observer2: observer 2

Observer Mode C # implementation
# Region observer mode /// <summary> /// base class to be observed /// </Summary> public abstract class subjectbase {private arraylist _ Observers = new arraylist (); public void add (iobserver O) {_ observers. add (o);} public void remove (iobserver O) {_ observers. remove (o);} public void Policy () {foreach (iobserver o in _ observers) {o. notify () ;}}/// <summary> // The Observer (delegate) /// </Summary> public class subject: subjectbase {}/// <summary> // The Observer implements the interface (event) /// </Summary> Public interface iobserver {void Policy ();} /// <summary> /// observer 1 /// </Summary> public class observer1: iobserver {public void Policy () {console. writeline ("observer 1") ;}/// <summary> // observer 2 /// </Summary> public class observer2: iobserver {public void Policy () {console. writeline ("observer 2") ;}# endregion

Test code

            Subject subject = new Subject();            subject.Add(new Observer1());            subject.Add(new Observer2());            subject.Notify();

Result

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.