static void Main(string[] args) { Observable obe = new ConcreteObservable(); ConcreteObserver concreteObserver_1 = new ConcreteObserver("ConcreteObserver_1"); ConcreteObserver concreteObserver_2 = new ConcreteObserver("ConcreteObserver_2"); ConcreteObserver concreteObserver_3 = new ConcreteObserver("ConcreteObserver_3"); obe.AddObserver(concreteObserver_1); obe.AddObserver(concreteObserver_3); obe.NotifyObservers(); obe.NotifyObservers(); obe.RemoveObserver(concreteObserver_3); obe.AddObserver(concreteObserver_2); obe.NotifyObservers(); obe.AddObserver(concreteObserver_3); obe.NotifyObservers(); PrintObserverInfo(concreteObserver_1); PrintObserverInfo(concreteObserver_2); PrintObserverInfo(concreteObserver_3); }
public void Update(object sub) { ObserverState newState = (ObserverState)sub; if (newState == ObserverState.Standby) { m_concreteSubject.DetachAll(); m_concreteSubject.StopUpdateByMyself(); m_concreteSubject = null; m_concreteObserver = null; //watch1_button.Enabled = false; buttonDoObserve.Text = "Do Observe"; } else { m_concreteSubject = new CurrentTimeSubject(); m_concreteObserver = new ConcreteObserver("Observe On Trace"); m_concreteSubject.Attach(m_concreteObserver); m_concreteSubject.StartUpdateByMyself(); //watch1_button.Enabled = true; buttonDoObserve.Text = "Stop Observe"; } }
static void Main(string[] args) { ISubject subject = new Subject(); ConcreteObserver obs = new ConcreteObserver(subject); subject.Notify(); Console.ReadLine(); }
public static void Main(string[] args) { var subject = new ConcreteSubject(); var observer1 = new ConcreteObserver(subject); var observer2 = new ConcreteObserver(subject); subject.Name = "panicoenlaxbox"; }
static void Main() { // Создание издателя. ConcreteSubject subject = new ConcreteSubject(); // Создание подписчиков. ConcreteObserver observer1 = new ConcreteObserver("1", subject); ConcreteObserver observer2 = new ConcreteObserver("2", subject); ConcreteObserver observer3 = new ConcreteObserver("3", subject); ConcreteObserver observer4 = new ConcreteObserver("4", subject); // Подписание подписчиков на издателя с получением объекта для отписки. IDisposable unsubscriber1 = subject.Subscribe(observer1); IDisposable unsubscriber2 = subject.Subscribe(observer2); IDisposable unsubscriber3 = subject.Subscribe(observer3); IDisposable unsubscriber4 = subject.Subscribe(observer4); using (subject) { // Попытка предоставить обзерверам некорректное состояние. subject.State = null; subject.Notify(); Console.WriteLine(new string('-', 70) + "1"); // Отписка первого подписчика через ConcreteSubject.Unsubscriber.Dispose() using (unsubscriber1) { // Попытка предоставить обзерверам корректное состояние. subject.State = "State 1 ..."; subject.Notify(); } Console.WriteLine(new string('-', 70) + "2"); // State 2 - получат только три подписчика которые остались подписанными. subject.State = "State 2 ..."; subject.Notify(); Console.WriteLine(new string('-', 70) + "3"); // Отписка второго подписчика через ConcreteObserver.OnCompleted() observer2.OnCompleted(); // State 3 - получат только 2 подписчика которые остались подписанными. subject.State = "State 3 ..."; subject.Notify(); } // observers.Clear() Console.WriteLine(new string('-', 70) + "4"); // Попытка отписать уже отписанного подписчика, обрабатывается в // ConcreteSubject.Unsubscriber.Dispose() observer4.OnCompleted(); // Delay. Console.ReadKey(); }
public static void Main(string[] args) { ConcreteObserver observer1 = new ConcreteObserver(); ConcreteObserver observer2 = new ConcreteObserver(); ConcretePublisher concretePublisher = new ConcretePublisher("teste"); concretePublisher.AttachObserver(observer1); concretePublisher.AttachObserver(observer2); concretePublisher.ChangeValue("test2"); }
static void Main(string[] args) { ConcreteSubject cs = new ConcreteSubject(); ConcreteObserver co = new ConcreteObserver(); AnotherConcreteObserver ao = new AnotherConcreteObserver(); cs.Register(co); cs.Register(ao); cs.SendAnUpdate("Wow what an amazing update!"); }
static void Main(string[] args) { ConcreteSubject sub = new ConcreteSubject(); ConcreteObserver obs = new ConcreteObserver(); sub.AddObserver(obs); sub.ChangeState(1); sub.RemoveObserver(obs); Console.ReadLine(); }
static void Main(string[] args) { ConcreteSubject cel = new ConcreteSubject(); ConcreteSubject cel2 = new ConcreteSubject(); Observer obs1 = new ConcreteObserver("Obserwator 1", cel); Observer obs2 = new ConcreteObserver("Obserwator 2", cel); Observer obs3 = new ConcreteObserver("Obserwator 3", cel2); Observer obs4 = new ConcreteObserver("Obserwator 4", cel2); cel.SetState("nowy stan"); cel2.SetState("kolejny stan"); }
static void Main(string[] args) { ConcreteSubject subject = new ConcreteSubject(); subject.State = "Мурзилка"; ConcreteObserver observer = new ConcreteObserver(subject, "Вася"); subject.Attach(observer); subject.Notify(); System.Console.WriteLine($"{observer.Name} подписан на журнал {observer.ObserverState}"); }
static void Main() { ConcreteSubject aConcreteSubject = new ConcreteSubject(); ConcreteObserver aConcreteObserver = new ConcreteObserver(aConcreteSubject); ConcreteObserver anotherConcreteObserver = new ConcreteObserver(aConcreteSubject); aConcreteSubject.aConcreteObserver = aConcreteObserver; aConcreteSubject.anotherConcreteObserver = anotherConcreteObserver; aConcreteObserver.ChangeState("First state!"); Console.WriteLine(new string('-', 70)); anotherConcreteObserver.ChangeState("Second state!"); }
/// <summary> /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { //先创建通知者 ConcreteSubject subject = new ConcreteSubject(); //在创建观察者 ConcreteObserver observer1 = new ConcreteObserver(subject, "学生小明"); //通知者添加观察者 subject.Attach(observer1); //改状态 //subject.SubjectState = "学习"; //通知 subject.Notify(); Console.ReadKey(); }
private static void Main(string[] args) { // canonical example var subject = new ConcreteSubject(); var observer1 = new ConcreteObserver(1); var observer2 = new ConcreteObserver(2); subject.Attach(observer1); subject.Attach(observer2); subject.SetState(new List <string>()); subject.UpdateStatePartially("yoyo"); subject.UpdateStatePartially("ahahha"); }
static void Main(string[] args) { //// 櫃台小姐童子婕 //Secretary secretary = new Secretary(); //// 看股票的同事 //StockObserver colleague1 = new StockObserver("同事1", secretary); //NBAObserver colleague2 = new NBAObserver("同事2", secretary); //// 櫃台記下了兩位同事 //secretary.Attach(colleague1); //secretary.Attach(colleague2); //// 發現老闆回來 //secretary.SubjectState = "老闆回來囉!"; //// 通知兩個同事 //secretary.Notify(); // 老闆胡漢三 Boss boss = new Boss(); // 看股票的同事 StockObserver colleague1 = new StockObserver("同事1", boss); NBAObserver colleague2 = new NBAObserver("同事2", boss); boss.Update += new EventHandler(colleague1.CloseStockMarket); boss.Update += new EventHandler(colleague2.CloseNBADirectSeeding); // 老闆回來 boss.SubjectState = "我胡漢三回來了!"; // 發出通知 boss.Notify(); Console.WriteLine("\n"); ConcreteSubject concreteSubject = new ConcreteSubject(); ConcreteObserver concreteObserver = new ConcreteObserver(concreteSubject, "testSubject"); concreteSubject.Update += new EventHandler(concreteObserver.Function); concreteSubject.SubjectState = "ABC"; concreteSubject.SubjectState = "CBA"; Console.ReadLine(); }
static void Main(string[] args) { ConcreteObserver obs1 = new ConcreteObserver("obs1"); ConcreteObserver obs2 = new ConcreteObserver("obs2"); List <IObserver> observers = new List <IObserver>() { obs1, obs2 }; Subject subject = new Subject(); subject.RegisterObserver(obs1); subject.RegisterObserver(obs2); subject.State = "state 1"; subject.UnregisterObserver(obs1); subject.State = "state 2"; }
static void Main(string[] args) { ConcreteSubject subject = new ConcreteSubject(); Observer observer1 = new ConcreteObserver(); Observer observer2 = new ConcreteObserver(); Observer observer3 = new ConcreteObserver(); Observer observer4 = new ConcreteObserver(); subject.Attach(observer1); subject.Attach(observer2); subject.Attach(observer3); subject.Attach(observer4); subject.Invoke(); Console.ReadKey(); }
static void Main(string[] args) { var producer1 = new ConcreteProducer("Producer #1"); var producer2 = new ConcreteProducer("Producer #2"); var observer1 = new ConcreteObserver("Observer #1"); var observer2 = new ConcreteObserver("Observer #2"); var observer3 = new ConcreteObserver("Observer #3"); // Producer1 addes only observer 1 producer1.AddObserver(observer1); // Producer2 is much more popular and adds all observers producer2.AddObserver(observer1); producer2.AddObserver(observer2); producer2.AddObserver(observer3); // When producer1 notifies we only see one action in the console producer1.Notify("Hello faithful subjects"); // When producer2 notifies we see 3 actions occur in the console producer2.Notify("Hello fellow citizens"); }
static void Main(string[] args) { var subject = new Subject(); var observerA = new ConcreteObserver(); subject.Attach(observerA); var observerB = new ConcreteObserverB(); subject.Attach(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.Detach(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); Console.ReadKey(); }
static void PrintObserverInfo(ConcreteObserver observer) { Console.WriteLine("{0}:", observer.Name); foreach (var evt in observer.Events) Console.WriteLine("\t{0}", evt); }
private void Chb2_Checked(object sender, RoutedEventArgs e) { ob2 = new ConcreteObserver(subject, Ell); subject.Attach(ob2); }
private void Chb3_Checked(object sender, RoutedEventArgs e) { ob3 = new ConcreteObserver(subject, RecRec); subject.Attach(ob3); }