static void Main(string[] args) { // Subject s = new Subject(); Subject s = new Subject(); IObserver ob1 = new Ob1(s); IObserver ob2 = new Ob2(); s.state = "状态1"; s.AddObserver(ob1); s.AddObserver(ob2); s.Update(); s.state = "状态2"; s.RemoveObserver(ob2); s.Update(); //DelegateSubject ds = new DelegateSubject(); //ds.Observer += ob1.Execute; //ds.Observer += ob2.Execute; //ds.Update(); //ds.Observer -= ob1.Execute; //ds.Update(); Console.Read(); }
void Test() { var subject1 = new Subject() { }; var observer_A = new Observer("A"); var observer_B = new Observer("B"); var observer_C = new Observer("C"); subject1.AddObserver(observer_A); subject1.AddObserver(observer_B); subject1.AddObserver(observer_C); subject1.Update(); }