Design Pattern NOTE 8: Observer mode

Source: Internet
Author: User

1.1 Definitions

Defines a one-to-many dependency that allows multiple observers to listen to an object at the same time, but when the object changes, all observer objects are notified so that they can update themselves.

1.2 Class Diagram

1.3 Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceObserver Pattern8 {9     //DelegateTen     Delegate voidEventHandler (); One  A     //At this point, if you need to add an NBA colleague, you need to modify the original class, contrary to the open closure principle, and at this time they are dependent on the details and against the principle of dependency reversal -     //so we're abstracting a parent class to solve this problem. -  the     //Observer Parent Class -     Abstract classObserver -     { -         protected stringname; +         protectedSubject Sub; -  +          PublicObserver (stringname, Subject sub) A         { at              This. Name =name; -              This. Sub =Sub; -         } -  -         //Public abstract void Update (); -     } in  -     //See stock watchers to     classStockobserver:observer +     { -  the          PublicStockobserver (stringname, Subject sub) *:Base(name, sub) $         {Panax Notoginseng  -         } the         //Notice +          Public voidClosestockmarket () A         { theConsole.WriteLine ("{0} {1} Close stock Quotes and continue working! ", Sub. Subjectstate, This. Name); +         } -     } $  $  -     //Watch NBA watchers -     classNbaobserver:observer the     { -          PublicNbaobserver (stringName, Subject sub):Base(name, sub) {}Wuyi          Public voidClosenbaplay () the         { -Console.WriteLine ("{0} {1} Close the NBA Live, continue to work! ", Sub. Subjectstate, This. Name); Wu         } -     } About  $  -  -     //Notifier Interface -     Abstract classSubject A     { +         //Delegate the          PublicEventHandler Update; -          Public Abstract voidNotify (); $          Public stringsubjectstate {Get;Set; } the     } the  the     //The boss implements the Notifier interface the     classBoss:subject -     { in          Public Override voidNotify () the         { the Update (); About         } the     } the  the     //Front desk Clerk +     //The front desk Secretary class is also a specific dependency, because the object of the notification may be the boss or other object. So it needs to be abstracted.  -     classSecretary:subject the     {Bayi         //Notice the          Public Override voidNotify () the         { - Update (); -         } the     } the  the  the  -  the}
View Code

Call:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceObserver Pattern8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             //Front desk -Subject s =NewBoss (); the             //colleagues who look at shares -Stockobserver T1 =NewStockobserver ("Big Bear", s); -Nbaobserver t2 =NewNbaobserver ("Kumaji", s); -  +             //Front desk Records two colleagues -             //S.attach (t1); +             //S.attach (T2); A             //S.detach (t1);//Bear not been notified to at             //The boss came back to inform his colleagues -S.update + =NewEventHandler (t1. Closestockmarket); -S.update + =NewEventHandler (T2. Closenbaplay); -S.subjectstate ="i Huhan three back! "; - s.notify (); -  in         } -     } to}
View Code

1.4 Summary

We define both the observer and the notifier and the notifier two abstract parent classes are intended for abstract programming, adding new classes later when adding new functionality, without modifying the original subclasses. There is also a coupling: The method that our Notifier object executes when notifying the observer is the same method. Although the execution of the method has been rewritten by the Observer, the name is consistent. So we added a member of the delegate type to the notifier, simply registering the method that notifies the observer to the delegate member of the notifier. This makes it possible to fully customize the execution method.

  

Design Pattern NOTE 8: Observer mode

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.