示例#1
0
        static void Main(string[] args)
        {
            Subject subject = new Subject();

            ObserverClass observer1 = new ObserverClass("Observer1");

            subject.Subscribe(observer1);
            subject.Subscribe(new ObserverClass("Observer2"));
            subject.InventoryCount++;
            subject.Unsubscribe(observer1);
            subject.Subscribe(new ObserverClass("Observer3"));
            subject.InventoryCount++;
            Console.ReadLine();
        }
示例#2
0
 static void Main(string[] args)
 {
     Subject subject = new Subject(5);
     // Observer1 takes a subscription to the store
     var observer1 = new Observar("Observer 1");
     subject.Subscribe(observer1);
     // Observer2 also subscribes to the store
     subject.Subscribe(new Observar("Observer 2"));
     subject.Data++;
     // Observer1 unsubscribes and Observer3 subscribes to notifications.
     subject.Unsubscribe(observer1);
     subject.Subscribe(new Observar("Observer 3"));
     subject.Data++;
     Console.ReadLine();
 }
        static void Main(string[] args)
        {
            Subject subject = new Subject();
            // Observer1 takes a subscription to the store
            Observer observer1 = new Observer("Observer 1");

            subject.Subscribe(observer1);
            // Observer2 also subscribes to the store
            subject.Subscribe(new Observer("Observer 2"));
            subject.Inventory++;
            // Observer1 unsubscribes and Observer3 subscribes to notifications.
            subject.Unsubscribe(observer1);
            subject.Subscribe(new Observer("Observer 3"));
            subject.Inventory++;
            Console.ReadLine();
        }
示例#4
0
        static async Task Main(string[] args)
        {
            IObserver consoleObserver = new ConsoleObserver();
            IObserver fileObserver    = new FileObserver();

            Subject subject = new Subject();

            subject.Subscribe(consoleObserver);
            subject.Subscribe(fileObserver);

            await subject.DoImportantWorkAsync();

            subject.Unsubscribe(fileObserver);

            await subject.DoImportantWorkAsync();
        }
        static void Main(string[] args)
        {
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();
            var observerB = new ConcreteObserverB();

            subject.SomeBusinessLogic();

            subject.Subscribe(observerA);

            subject.Subscribe(observerB);

            subject.SomeBusinessLogic();

            subject.SomeBusinessLogic();

            subject.Unsubscribe(observerB);

            subject.SomeBusinessLogic();
        }