public ObserverTest() { _subject = new ConcreteSubject(); _observerA = new ConcreteObserverA(_subject); _observerB = new ConcreteObserverB(_subject); _subject.Attach(_observerA); _subject.Attach(_observerB); }
private void Start() { ConcreteSubjectA subjectA = new ConcreteSubjectA(); IObserver observerA = new ConcreteObserverA(subjectA); IObserver observerB = new ConcreteObserverB(subjectA); subjectA.AddObserber(observerA); subjectA.AddObserber(observerB); //改变subjectA的状态 subjectA.State = "状态A"; }
public void RunTest() { var o1 = new ConcreteObserverA(); var o2 = new ConcreteObserverB(); var subject = new Subject(); subject.RegisterObserver(o1); subject.RegisterObserver(o2); subject.NotifyListeners(); Console.WriteLine("Listeners notified!"); Assert.Pass(); }
static void Main(string[] args) { var subject = new Subject(); var concreteObserverA = new ConcreteObserverA(); var concreteObserverB = new ConcreteObserverB(); subject.Notify += concreteObserverA.Update; subject.Notify += concreteObserverB.Update; subject.StateDelegate += concreteObserverA.Update; subject.StateDelegate += concreteObserverB.Update; subject.SomeBusinessLogic(); }
static void Main(string[] args) { //Observer Pattern var subject = new Subject(); var observerA = new ConcreteObserverA(); subject.Attach(observerA); var observerB = new ConcreteObserverB(); subject.Attach(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.Detach(observerB); subject.SomeBusinessLogic(); }
private static void DemonstrateObserver() { var subject = new Subject(); var observerA = new ConcreteObserverA(); var observerB = new ConcreteObserverB(); observerA.Subscribe(subject); observerB.Subscribe(subject); subject.AddItem(2); subject.AddItem(3); observerA.Unsubscribe(); subject.AddItem(5); observerB.Unsubscribe(); }
public static void RunPubSub() { var subject = new Subject(); var observerA = new ConcreteObserverA(); subject.Attach(observerA); var observerB = new ConcreteObserverB(); subject.Attach(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.Detach(observerB); subject.SomeBusinessLogic(); }
public static void ClientCode() { var subj = new Subject(); var o1 = new ConcreteObserverA(); subj.attach(o1); var o2 = new ConcreteObserverB(); subj.attach(o2); subj.someBusinessLogic(); subj.someBusinessLogic(); subj.detach(o2); subj.someBusinessLogic(); }
public void ObserverTest() { // The client code. var subject = new Subject(); var observerA = new ConcreteObserverA(); subject.Attach(observerA); var observerB = new ConcreteObserverB(); subject.Attach(observerB); subject.SomeBusinessLogic().Should().BeInRange(0, 10); subject.SomeBusinessLogic().Should().BeInRange(0, 10); subject.Detach(observerB); subject.SomeBusinessLogic().Should().BeInRange(0, 10); }
public void Get() { // The client code. var subject = new Subject(); var observerA = new ConcreteObserverA(); subject.Attach(observerA); var observerB = new ConcreteObserverB(); subject.Attach(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.Detach(observerB); subject.SomeBusinessLogic(); }
static void MainABC(string[] args) { // The client code. var subject = new ConcreteSubject(); var observerA = new ConcreteObserverA(); subject.Attach(observerA); var observerB = new ConcreteObserverB(); subject.Attach(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.Detach(observerB); subject.SomeBusinessLogic(); }
static void Main(string[] args) { // The client code. var subject = new Subject(); var observerA = new ConcreteObserverA(); subject.AddOberver(observerA); var observerB = new ConcreteObserverB(); subject.AddOberver(observerB); subject.SomeBusinessLogic(); subject.SomeBusinessLogic(); subject.DeleteOberver(observerB); subject.SomeBusinessLogic(); }
private void RunObserverTest() { ConcreteObservable someProcess = new ConcreteObservable(); for (int i = 0; i < 10; i++) { if (i % 2 == 0) { var tempObject = new ConcreteObserverA(String.Format("ObserverA{0}", i), someProcess); } else { var tempObject = new ConcreteObserverB(String.Format("ObserverB{0}", i), someProcess); } } for (int i = 0; i < 10; i++) { someProcess.ReinitInnerState(); } }