Abstract notification Subject class

Source: Internet
Author: User

When the observer changes, all the observers are notified through the Notify interface. Avoid direct implementation and reduce coupling. [Csharp] <span style = "font-size: 12px;"> abstract class Subject {private IList <Observer> observers = new List <Observer> (); // Add public void Attach (Observer observer) {observers. add (observer);} // remove the Observer public void Detach (observer) {observers. remove (observer);} // Notify public void Policy () {foreach (Observer o in observers) {o. update () ;}}</span> abstract observer Observers class calls Update when a topic notification is received. Called the Update interface [csharp] <span style = "font-size: 10px;"> abstract class Observer {public abstract void Update ();} abstract class Subject </span> the concreteObserver class of the observer implements the update interface of the abstract observer to update the current state. [Csharp] class ConcreteObserver: Observer {private string name; private string observerState; private ConcreteSubject subject; public ConcreteObserver (ConcreteSubject subject, string name) {this. subject = subject; this. name = name;} public override void Update () {observerState = subject. subjectState; Console. writeLine ("The Observer {0} status is {1}", name, observerState);} client [csharp] static void Main (string [] args) {ConcreteSubject a = new ConcreteSubject ();. attach (new ConcreteObserver (a, "B");. subjectState = "state a";. Y (); Console. read ();}

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.