Esempio n. 1
0
        public static void Behavioral_ObserverDemo2()
        {
            var statusBar     = new StatusBar();
            var stockListView = new StockListView();

            var stock1 = new Stock("stock1", 10);
            var stock2 = new Stock("stock2", 20);
            var stock3 = new Stock("stock3", 30);

            // The status bar shows the popular stocks
            statusBar.AddStock(stock1);
            statusBar.AddStock(stock2);

            // The stock view list shows all stocks
            stockListView.AddStock(stock1);
            stockListView.AddStock(stock2);
            stockListView.AddStock(stock3);

            // Causes both statusBar and stockListView to get refreshed
            stock2.SetPrice(21);

            // Causes only the stockListView to get refreshed. (statusBar
            // is not watching this stock.)
            stock3.SetPrice(9);
        }
Esempio n. 2
0
        public ObserverPatternMain()
        {
            // Stocks
            Stock stock1 = new Stock("EMR", 1);
            Stock stock2 = new Stock("SHS", 3);
            Stock stock3 = new Stock("JSK", 7);

            // Presentation Layers
            var statusBar     = new StatusBar();
            var stockListView = new StockListView();


            statusBar.AddStock(stock1);
            stockListView.AddStock(stock1);
            stockListView.AddStock(stock2);
            stockListView.AddStock(stock3);


            // stock1.Price = 10;
            stock2.Price = 12;
        }
        static void Main(string[] args)
        {
            // Pull Style Observer Pattern
            var pullStyleDataSource = new Solution.PullStyle.DataSource();
            var pullStyleChart      = new Solution.PullStyle.Chart(pullStyleDataSource);

            pullStyleDataSource.Attach(pullStyleChart);
            var pullStyleSpreadSheet = new Solution.PullStyle.SpreadSheet(pullStyleDataSource);

            pullStyleDataSource.Attach(pullStyleSpreadSheet);

            pullStyleDataSource.SetValue(10);

            pullStyleDataSource.Dettach(pullStyleSpreadSheet);

            pullStyleDataSource.SetValue(50);

            // Push Style Observer Pattern
            var pushStyleDataSource = new Solution.PushStyle.DataSource();
            var pushStyleChart      = new Solution.PushStyle.Chart();

            pushStyleDataSource.Attach(pushStyleChart);
            var pushStyleSpreadSheet = new Solution.PushStyle.SpreadSheet();

            pushStyleDataSource.Attach(pushStyleSpreadSheet);

            pushStyleDataSource.SetValue(10);

            pushStyleDataSource.Dettach(pushStyleSpreadSheet);

            pushStyleDataSource.SetValue(50);

            Console.WriteLine("--- Exercise ---");

            var listOfStocks = new List <Stock>
            {
                new Stock("Stock1", 10),
                new Stock("Stock2", 20),
                new Stock("Stock3", 30),
            };

            var statusBar = new StatusBar();

            statusBar.AddStock(listOfStocks[0]);
            statusBar.AddStock(listOfStocks[1]);
            var stockListView = new StockListView();

            stockListView.AddStock(listOfStocks[0]);
            stockListView.AddStock(listOfStocks[1]);
            stockListView.AddStock(listOfStocks[2]);

            listOfStocks[0].SetPrice(15);
            listOfStocks[1].SetPrice(25);
            listOfStocks[2].SetPrice(35);

            listOfStocks[1].Dettach(statusBar);

            listOfStocks[0].SetPrice(25);
            listOfStocks[1].SetPrice(35);
            listOfStocks[2].SetPrice(45);

            Console.ReadLine();
        }