Observer Pattern mode

xiaoxiao2021-04-08  420

structure

Code

Using system; using system.collections;

A summary description of Namespace ExempleObserver {///

/// Subject. /// interface Subject {// method; void attach (observer observer); void notify ();

// "ConcreteSubject" class ConcreteSubject: Subject {// Fields private ArrayList observers = new ArrayList (); // Methods public void Attach (Observer observer) {observers.Add (observer);} public void Detach (Observer observer) {observers .Remove (observer);} public void Notify () {foreach (Observer o in observers) o.Update ();} // Fields private string subjectState; // Properties public string subjectState {get {return subjectState;} set {subjectState = value;}}} // "Observer" interface Observer {// Methods void Update ();} // "ConcreteObserver" class ConcreteObserver: Observer {// Fields private string name; private string observerState; private ConcreteSubject subject; // Constructors public ConcreteObserver (ConcreteSubject subject, string name) {this.subject = subject; this.name = name;} // Methods public void Update () {observerState = subject.SubjectState; Console.WriteLine ( "Observer {0} 's New stat e is {1} ", name, observerstate);} // Properties Public ConcreteSubject Subject;} set {subject = value;}}}} ///

/// Class1 summary description. /// class formmain {/// /// The primary entry point of the application.

转载请注明原文地址:https://www.9cbs.com/read-132717.html

New Post(0)