Esempio n. 1
0
        public static void Main_observer_V31(string[] args)
        {
            // Create objects for testing
            ICricketObserver iAverageScoreDisplay = new AverageScoreDisplay();

            iAverageScoreDisplay.Notify += AverageScoreDisplay_Notify;

            ICricketObserver iCurrentScoreDisplay = new CurrentScoreDisplay();

            iCurrentScoreDisplay.Notify += CurrentScoreDisplay_Notify;

            // Create Cricket Subject and pass initial data
            CricketSubject cricketSubject = new CricketSubject()
            {
                CricketData = new CricketData()
                {
                    Runs    = 100,
                    Wickets = 3,
                    Overs   = 22.3f
                }
            };

            ICricketSubject iCricketSubject = cricketSubject;

            iCricketSubject.Subscribed += CricketSubject_Subscribed;

            //Register display elements
            iCricketSubject.Subscribe(iCurrentScoreDisplay);
            iCricketSubject.Subscribe(iAverageScoreDisplay);


            //In real app you would have some logic to
            //call this function when data changes
            iCricketSubject.Update();

            //Remove an observer
            iCricketSubject.UnSubscribed += CricketSubject_UnSubscribed;
            iCricketSubject.UnSubscribe(iAverageScoreDisplay);

            cricketSubject.CricketData = new CricketData
            {
                Runs    = 130,
                Wickets = 4,
                Overs   = 26.2f
            };
            iCricketSubject = cricketSubject;

            // Now only currentScoreDisplay gets the notification
            iCricketSubject.Update();
            Console.ReadKey();
        }
        public static void Main_observer_V32(string[] args)
        {
            ICricketFactory iFactory = new CricketFactory();
            // Create objects for testing
            ICricketObserver iAverageScoreDisplay = iFactory.CreateAverageScoreDisplay();

            iAverageScoreDisplay.Notify += AverageScoreDisplay_Notify;

            ICricketObserver iCurrentScoreDisplay = iFactory.CreateCurrentScoreDisplay();

            iCurrentScoreDisplay.Notify += CurrentScoreDisplay_Notify;

            // Create Cricket Subject and pass initial data
            ICricketSubject cricketSubject = iFactory.CreateCicketSubject(
                (o) => {
                o.Runs    = 100;
                o.Wickets = 3;
                o.Overs   = 22.3f;
            });

            ICricketSubject iCricketSubject = cricketSubject;

            iCricketSubject.Subscribed += CricketSubject_Subscribed;

            //Register display elements
            iCricketSubject.Subscribe(iCurrentScoreDisplay);
            iCricketSubject.Subscribe(iAverageScoreDisplay);


            //In real app you would have some logic to
            //call this function when data changes
            iCricketSubject.Update();

            //Remove an observer
            iCricketSubject.UnSubscribed += CricketSubject_UnSubscribed;
            iCricketSubject.UnSubscribe(iAverageScoreDisplay);

            cricketSubject.Next(
                (o) =>
            {
                o.Runs    = 135;
                o.Wickets = 4;
                o.Overs   = 26.2f;
            });

            iCricketSubject = cricketSubject;

            // Now only currentScoreDisplay gets the notification
            iCricketSubject.Update();
            Console.ReadKey();
        }