static void Main(string[] args)
        {
            //This is our Observable also known as publisher that notifies about change
            var stockObservable = new Observable <Stock>();

            //observer that monitors microsoft stock
            var microsoftObserver = new MicrosoftStockObserver();

            //here microsoftObserver gets register with stockObservable
            //as microsoftObserver wants to get notified when there is a
            //change made inside the subject.
            stockObservable.Register(microsoftObserver);

            //observer that monitors google stock
            var googleObserver = new GoogleStockObserver();

            //here googleObserver gets register with stockObservable
            //as googleObserver wants to get notified when there is a
            //change made inside the subject.
            stockObservable.Register(googleObserver);

            //our same old simulator
            var stockSimulator = new StockSimulator();

            //code that updates the subject
            foreach (var stock in stockSimulator)
            {
                stockObservable.Subject = stock; //this will cause for
            }
            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var stockObservable = new Observable <Stock>();

            var microsoftObserver = new MicrosoftStockObserver();

            microsoftObserver.Subscribe(stockObservable);

            var googleObserver = new GoogleStockObserver();

            googleObserver.Subscribe(stockObservable);

            stockObservable.Subject = new Stock("Microsoft", 10);
            microsoftObserver.Unsubscribe();
            stockObservable.Subject = new Stock("Microsoft", 20);
            stockObservable.Subject = new Stock("Microsoft", 30);
            microsoftObserver.Subscribe(stockObservable);
            stockObservable.Subject = new Stock("Microsoft", 40);
            stockObservable.Subject = new Stock("Google", 60);
            googleObserver.Unsubscribe();
            stockObservable.Subject = new Stock("Google", 70);
            stockObservable.Subject = new Stock("Google", 80);
            googleObserver.Subscribe(stockObservable);
            stockObservable.Subject = new Stock("Google", 90);
            stockObservable.Error(new StockNotFoundException("Some error occured!"));
            stockObservable.Completed();
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            var stockObservable = new Observable <Stock>();

            var microsoftObserver = new MicrosoftStockObserver();

            microsoftObserver.Subscribe(stockObservable);

            var googleObserver = new GoogleStockObserver();

            googleObserver.Subscribe(stockObservable);

            stockObservable.Subject = new Stock("Microsoft", 10);
            microsoftObserver.Unsubscribe();
            stockObservable.Subject = new Stock("Microsoft", 20);
            stockObservable.Subject = new Stock("Microsoft", 30);
            microsoftObserver.Subscribe(stockObservable);
            stockObservable.Subject = new Stock("Microsoft", 40);
            stockObservable.Subject = new Stock("Google", 60);
            googleObserver.Unsubscribe();
            stockObservable.Subject = new Stock("Google", 70);
            stockObservable.Subject = new Stock("Google", 80);
            googleObserver.Subscribe(stockObservable);
            stockObservable.Subject = new Stock("Google", 90);

            Console.ReadLine();
        }
예제 #4
0
    static void Main(string[] args)
    {
        var stockObservable   = new StockObservable();
        var microsoftObserver = new MicrosoftStockObserver(stockObservable);
        var googleObserver    = new GoogleStockObserver(stockObservable);

        var stockSimulator = new StockSimulator();

        foreach (var stock in stockSimulator)
        {
            stockObservable.Subject = stock;
        }
        Console.ReadLine();
    }