Пример #1
0
        static void Main(string[] args)
        {
            // Observer Concept
            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();

            //Exercice 1
            var estacion = new EstacionMetereologica();
            var dipositivoTemperatura = new DispositivoTiempoActual();

            estacion.Attach(dipositivoTemperatura);
            for (int i = 0; i < 10; i++)
            {
                estacion.medidasHanCambiado();
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();
            var observerB = new ConcreteObserverB();

            subject.Attach(observerA);
            subject.Attach(observerB);

            subject.SomeBusinessLogic();
        }
Пример #3
0
        static void Main(string[] args)
        {
            ConcreteObservable concreteObservable = new ConcreteObservable();

            concreteObservable.Register(new ConcreteObserverA("First"));
            concreteObservable.Register(new ConcreteObserverB("Second"));
            var observerToRegister = new ConcreteObserverB("Third");

            concreteObservable.Register(observerToRegister);

            concreteObservable.NotifyObserver(24);
            concreteObservable.Unregister(observerToRegister);
            Console.WriteLine("==================================");
            concreteObservable.NotifyObserver(42);

            Console.ReadKey();
        }
Пример #4
0
        static void Main(string[] args)
        {
            var publisher = new Publisher();

            var observerA = new ConcreteObserverA();
            var observerB = new ConcreteObserverB();

            publisher.AddObserver(observerA);
            publisher.AddObserver(observerB);

            publisher.SomeBusinessLogic();
            publisher.SomeBusinessLogic();

            publisher.RemoveObserver(observerA);
            publisher.RemoveObserver(observerB);

            publisher.SomeBusinessLogic();
        }
Пример #5
0
        static void Main(string[] args)
        {
            //创建主题对象
            ConcreteSubject Bucciarati = new ConcreteSubject();

            //创建订阅者
            ConcreteObserverA Prosciutto = new ConcreteObserverA("普罗修特");
            ConcreteObserverB Pesci      = new ConcreteObserverB("贝西");

            //订阅者订阅主题对象(委托的多播)
            Bucciarati.Update += new EventHandler(Prosciutto.Attack);
            Bucciarati.Update += new EventHandler(Pesci.Shout);

            //主题对象通知事件
            Bucciarati.Notify();

            Console.Read();
        }
Пример #6
0
        public void Main()
        {
            // 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();
        }