zoukankan      html  css  js  c++  java
  • observertheory.cs

      using System;
      using System.Collections;
      using System.Threading;

      class ObserverPattern {
       
        //Observer Pattern            Judith Bishop Jan 2007
       
        // The Subject runs in a thread and changes its state
        // independently. At each change, it notifies its Observers.
       
        class Subject {
          public delegate void Callback (string s);
         
          public event Callback Notify;

          Simulator simulator = new Simulator();
          const int speed = 200;
          public string SubjectState {get; set;}
         
          public void Go() {
            new Thread(new ThreadStart(Run)).Start();
          }
         
          void Run () {
            foreach (string s in simulator) {
              Console.WriteLine("Subject: "+s);
              SubjectState = s;
              Notify(s);
              Thread.Sleep(speed); //millisconds
            }
          }
        }

        interface IObserver {
         void Update(string state);
        }

        class Observer : IObserver {
          string name;
          Subject subject;
          string state;
          string gap;
       
          public Observer (Subject subject, string name,string gap) {
            this.subject = subject;
            this.name = name;
            this.gap = gap;
            subject.Notify += Update;
          }
         
          public void Update(string subjectState) {
            state = subjectState;
            Console.WriteLine(gap+name+": "+state);
          }
        }
     
        static void Main () {
          Subject subject = new Subject();
          Observer Observer =  new Observer(subject,"Center","\t\t");
          Observer observer2 = new Observer(subject,"Right","\t\t\t\t");
          subject.Go();
        }
       
        class Simulator : IEnumerable {
     
        string [] moves = {"5","3","1","6","7"};

        public IEnumerator GetEnumerator () {
          foreach( string element in moves )
            yield return element;
          }
        }
      }
    /*Output
    Subject: 5
        Center: 5
            Right: 5
    Subject: 3
        Center: 3
            Right: 3
    Subject: 1
        Center: 1
            Right: 1
    Subject: 6
        Center: 6
            Right: 6

    */

  • 相关阅读:
    玩转JavaScript module pattern精髓
    玩转Javascript 给JS写测试
    Feature Toggle JUnit
    状态机模式实战
    Java静态类
    Guava增强for循环
    Spring Security使用心得
    听个响
    Geoserver2.16.2初步使用
    GeoWebCache1.10.5发布arcgis瓦片服务
  • 原文地址:https://www.cnblogs.com/shihao/p/2512060.html
Copyright © 2011-2022 走看看