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

            Observer o1 = new Observer(s);
            Observer o2 = new Observer(s);

            s.DoSomething();
            Console.ReadLine();
        }
示例#2
0
        static void Main()
        {
            var rnd       = new Random();
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();
            var observerB = new ConcreteObserverB();

            Parallel.For(0, 5, (i, loopState) =>
            {
                int delay;
                lock (rnd)
                    delay = rnd.Next(1, 1001);
                Thread.Sleep(delay);
                Console.WriteLine("Thread : " + i);
                subject.Attach(observerA);
                subject.DoSomething();
                subject.Detach(observerB);
            });
        }