Example #1
0
        public void StockGrabber_UpdatesObserver_IsCorrect()
        {
            StockGrabber stockGrabber = new StockGrabber();

            StockObserver.ResetObserverIdTracker();
            StockObserver observer1 = new StockObserver(stockGrabber);

            using (ConsoleOutput consoleOutput = new ConsoleOutput())
            {
                const string consoleOutputFormat   = "{0} \nIBM: {1}\nAAPL: {2}\nGOOG: {3}\n\n";
                string       consoleOutputExpected = string.Empty;

                stockGrabber.SetIBMPrince(197.00);
                consoleOutputExpected += string.Format(consoleOutputFormat, 1, 197.00, 0, 0);
                Assert.AreEqual(consoleOutputExpected, consoleOutput.GetOuput(false));

                stockGrabber.SetAaplPrince(677.00);
                consoleOutputExpected += string.Format(consoleOutputFormat, 1, 197.00, 677.00, 0);
                Assert.AreEqual(consoleOutputExpected, consoleOutput.GetOuput(false));

                stockGrabber.SetGoogPrince(676.00);
                consoleOutputExpected += string.Format(consoleOutputFormat, 1, 197.00, 677.00, 676.00);
                Assert.AreEqual(consoleOutputExpected, consoleOutput.GetOuput(false));
            }
        }
        public void TestMethod1()
        {
            IEnumerable <string> tickers       = TickerHelper.GetConcatenatedTickers(@"C:\StockData\SymbolsTop100.csv", new char[] { '\n', '\r' }, ',', 5);
            StockObserver        stockObserver = new StockObserver(tickers, "*****@*****.**", 0.0, 5, 1);

            for (int i = 0; i < 2; i++)
            {
                stockObserver.Observe();
                Thread.Sleep(5000);
            }
        }
Example #3
0
        private static void Main(string[] args)
        {
            var grabber  = new StockGrabber();
            var obsever1 = new StockObserver(grabber);

            grabber.SetIbmPrice(200);
            grabber.SetApplePrice(500);
            grabber.SetGooglePrice(260);

            grabber.SetIbmPrice(260);
            grabber.SetApplePrice(510);
            grabber.SetGooglePrice(280);
        }
Example #4
0
        static void Main(string[] args)
        {
            Boss huhansan = new Boss();

            StockObserver stockObserver = new StockObserver("魏关姹", huhansan);
            NBAObserver   nbaObserver   = new NBAObserver("易管查", huhansan);

            huhansan.Update += new EventHandler(stockObserver.CloseStockMarket);
            huhansan.Update += new EventHandler(nbaObserver.CloseNBADirectSeeding);

            huhansan.SubjectState = "我胡汉三回来了!";
            huhansan.Notify();
        }
Example #5
0
        public void TestObserver_Example()
        {
            Boss myBoss = new Boss();

            StockObserver user1 = new StockObserver("leo", myBoss);

            NBAObserver user2 = new NBAObserver("ken", myBoss);

            myBoss.Update += new MyEventHandler(user1.CloseStockMarket);
            myBoss.Update += new MyEventHandler(user2.CloseNBADirectSeeding);

            myBoss.SubjectState = "I'm come back!!! ";
            myBoss.Notify();
        }
Example #6
0
    void TestObserver()
    {
        Secretary newSecretary = new Secretary();

        StockObserver stockGuy = new StockObserver("Stock Guy", newSecretary);
        NBAObserver   nbaGuy   = new NBAObserver("NBA Guy", newSecretary);

        newSecretary.Attach(stockGuy);
        newSecretary.Attach(nbaGuy);

        newSecretary.DeTach(stockGuy);

        newSecretary.Nofity();
    }
Example #7
0
        /// <summary>
        /// 事件委托实现
        /// </summary>
        static void TestDelegate()
        {
            Boss huhansan = new Boss();

            StockObserver tongshi1 = new StockObserver("xiaohuang", huhansan);
            NBAObserver   tongshi2 = new NBAObserver("xiaoming", huhansan);

            // 1.一个委托可以搭载多个方法,所有方法被依次唤起
            // 2.委托对象所搭载的方法并不需要属于同一个类
            huhansan.Update += new MYEventHandler(tongshi1.CloseStockmarket);
            huhansan.Update += new MYEventHandler(tongshi2.CloseNBADirectSeeding);

            huhansan.SubjectState = "come back";
            huhansan.Notify();
        }
Example #8
0
        public Demo()
        {
            var boss          = new Boss();
            var stockObserver = new StockObserver("小魏", boss);
            var nbaObserver   = new NbaObserver("小易", boss);
            var otherObserver = new OtherObserver();

            boss.Update += stockObserver.CloseStockMarket;
            boss.Update += nbaObserver.CloseNbaDirectSeeding;
            boss.Update += otherObserver.Other;

            boss.SubjectState = "我胡汉三回来了";
            //boss.Update -= stockObserver.CloseStockMarket;
            boss.Notify();
        }
Example #9
0
        static void Main(string[] args)
        {
            //Dictionary<string, Fields> dict = YahooApi.yahooGetQuotes("AAPL,YHOO",true);
            //Dictionary<string, Fields> dict1 = YahooApi.yahooGetQuotes("AAPL,YHOO");
            Trace.TraceInformation("Starting Stock observing application!");
            IEnumerable <string> tickers       = TickerHelper.GetConcatenatedTickers(@"C:\StockData\SymbolsTop100.csv", new char[] { '\n', '\r' }, ',', 300);
            StockObserver        stockObserver = new StockObserver(tickers, "[email protected],[email protected]", 5.0, 15, 100);

            while (true)
            {
                stockObserver.Observe();
                Thread.Sleep(60000);
            }
            //Console.WriteLine(EmailManager.SendEmailAlert("This means emailNotificationManager is working in tradeSystm","[email protected];[email protected];[email protected]"));
            //Implement log to log emails sent, triggers etc
        }
Example #10
0
        static void Main(string[] args)
        {
            Secretray     secretray     = new Secretray();
            StockObserver stockObserver = new StockObserver("小李", secretray);

            secretray.Attach(stockObserver);
            secretray.SubjectState = "老板回来了";
            secretray.Notify();

            Boss        boss        = new Boss();
            NbaObserver nbaObserver = new NbaObserver("小王", boss);

            boss.Update      += new ObserverDemo.Demo2.EventHandler(nbaObserver.CloseNBATv);
            boss.SubjectState = "我回来了";
            boss.Notify();
            Console.ReadLine();
        }
        public void Test_Observer_BossExample()
        {
            Boss anguryBoss = new Boss();

            StockObserver worker1 = new StockObserver("thkouob", anguryBoss);
            NBAObserver   worker2 = new NBAObserver("zf", anguryBoss);

            ////anguryBoss.Attach(worker1);
            ////anguryBoss.Attach(worker2);

            ////anguryBoss.Detach(worker1);
            anguryBoss.Update += new MyEventHandler(worker1.Update);
            anguryBoss.Update += new MyEventHandler(worker2.Update);

            anguryBoss.SubjectState = "Boss is come back!";

            anguryBoss.Notify();
        }
Example #12
0
        public static void RunObserver()
        {
            StockGrabber grabber = new StockGrabber();

            StockObserver o1 = new StockObserver(grabber);

            grabber.setIBMPrice(197);
            grabber.setAAPLPrice(667);
            grabber.setGOOGPrice(676);

            StockObserver o2 = new StockObserver(grabber);

            grabber.setIBMPrice(197);
            grabber.setAAPLPrice(667);
            grabber.setGOOGPrice(676);

            grabber.Unregister(o1);
        }
Example #13
0
        //观察者
        public static void testObserver()
        {
            //老板胡汉三
            Boss huhansan = new Boss();

            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", huhansan);
            //看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", huhansan);

            huhansan.Update += new DesignPatten.BehaviorType.EventHandler(tongshi1.CloseStockMarket);
            huhansan.Update += new DesignPatten.BehaviorType.EventHandler(tongshi2.CloseNBADirectSeeding);

            //老板回来
            huhansan.SubjectState = "我胡汉三回来了!";
            //发出通知
            huhansan.Notify();

            Console.Read();
        }
Example #14
0
        private static void Test2()
        {
            // 老板胡汉三
            Boss huHanSan = new Boss();

            // 看股票的同事
            StockObserver stockFriend = new StockObserver("张三", huHanSan);

            // 看NBA的同事
            NBAObserve nbaFriend = new NBAObserve("李四", huHanSan);

            // 挂载方法
            huHanSan.Update += stockFriend.CloseStockMarket;
            huHanSan.Update += nbaFriend.CloseNBADirectSeeding;

            // 老板回来
            huHanSan.SubjectState = "我胡汉三回来了!";

            // 发出通知
            huHanSan.Notify();
        }
Example #15
0
        public static void Main()
        {
            var stockGrabber = new StockGrabber();

            var observer1 = new StockObserver(stockGrabber);

            stockGrabber.SetIbmPrice(154.40);
            stockGrabber.SetAaplPrice(651.10);
            stockGrabber.SetGoogPrice(321.11);

            var observer2 = new StockObserver(stockGrabber);

            stockGrabber.SetIbmPrice(154.40);
            stockGrabber.SetAaplPrice(651.10);
            stockGrabber.SetGoogPrice(321.11);

            stockGrabber.Unregister(observer1);

            stockGrabber.SetIbmPrice(154.40);
            stockGrabber.SetAaplPrice(651.10);
            stockGrabber.SetGoogPrice(321.11);
        }
Example #16
0
        static void Main(string[] args)
        {
            #region Strategy
            Console.WriteLine("------------Strategy------------------");
            Console.WriteLine("\r\n ------Strategy Test");
            {
                CashNormal cn = new CashNormal();
                Context    ct = new Context(cn);
                ct.PayAmount(100);

                CashDiscount cd = new CashDiscount(0.1);
                ct = new Context(cd);
                ct.PayAmount(100);

                CashReturn cr = new CashReturn(300, 100);
                ct = new Context(cr);
                ct.PayAmount(400);
            }
            #endregion

            #region Observer
            Console.WriteLine("------------Observer------------------");
            Console.WriteLine("\r\n ------Observer Test");
            SydneyStock   ss  = new SydneyStock();
            StockObserver ob1 = new StockObserver();
            ss.Register(ob1);
            ss.Notify(100);
            ss.Register(new StockObserver());
            ss.Notify(200);
            ss.Unregister(ob1);
            ss.Notify(300);
            #endregion

            #region Factory
            Console.WriteLine("------------Factory------------------");
            Console.WriteLine("\r\n ------Factory Test");
            WarshipFactory wf = new WarshipFactory();
            String         s  = "A";
            Warship        ws = wf.GetWarship(s);
            if (ws != null)
            {
                ws.Fight();
            }

            s  = "B";
            ws = wf.GetWarship(s);
            if (ws != null)
            {
                ws.Fight();
            }
            #endregion

            #region Abstract Factory
            Console.WriteLine("------------Abstract Factory------------------");
            Console.WriteLine("\r\n ------Abstract Factory Test");
            IRestaurant ir = new ChineseRestaurant();
            IVegetable  iv = ir.CookVegetable();
            iv.Display();
            IMeat im = ir.CookMeat();
            im.Display();

            ir = new AustralianRestaurant();
            iv = ir.CookVegetable();
            iv.Display();
            im = ir.CookMeat();
            im.Display();
            #endregion

            #region Singleton
            Console.WriteLine("------------Singleton------------------");
            Console.WriteLine("\r\n ------Singleton Test");
            MySingleton ms1 = MySingleton.GetInstance();
            MySingleton ms2 = MySingleton.GetInstance();
            if (ms1 == ms2)
            {
                Console.WriteLine("Same Instance of Singleton");
            }
            #endregion

            #region Builder
            Console.WriteLine("------------Builder------------------");
            Console.WriteLine("\r\n ------Builder Test");
            {
                IPersonBuilder        pb  = new FatPersonBuilder();
                PersonBuilderDirector pbd = new PersonBuilderDirector(pb);
                pbd.BuildPerson();
                pbd = new PersonBuilderDirector(new ThinPersonBuilder());
                pbd.BuildPerson();
            }
            #endregion

            #region AdvanceBuilder
            Console.WriteLine("\r\n ------AdvanceBuilder Test");
            {
                PersonBuilderDirectorBase pbd = new PersonBuilderDirector1();
                pbd.SetPersonBuilder(new FatPersonBuilder());
                pbd.BuildPerson();
                pbd.SetPersonBuilder(new ThinPersonBuilder());
                pbd.BuildPerson();
                Console.WriteLine();
                pbd = new PersonBuilderDirector2();
                pbd.SetPersonBuilder(new FatPersonBuilder());
                pbd.BuildPerson();
                pbd.SetPersonBuilder(new ThinPersonBuilder());
                pbd.BuildPerson();
            }
            #endregion

            #region Prototype
            Console.WriteLine("------------Prototype------------------");
            Resume r1 = new Resume();
            r1.id   = 1;
            r1.Name = "P1";
            r1.Experience.Company = "C1";
            Console.WriteLine(r1);
            Resume r2 = (Resume)r1.Clone();
            r2.id   = 2;
            r2.Name = "P2";
            r2.Experience.Company = "C2";
            Console.WriteLine(r2);
            #endregion

            #region Decorator
            Console.WriteLine("------------Decorator------------------");
            Pizza        pp = new PlainPizza();
            ToppingPizza tp = new Mozzarella(pp);
            ToppingPizza ts = new TomatoSauce(tp);
            ts.GetDescription();

            #endregion

            #region Command
            Console.WriteLine("------------Command------------------");
            Cook            barbecuerCook    = new BarbecuerCook();
            Cook            fryDishCook      = new FryDishCook();
            Command.Command barbecuerCommand = new BarbecuerCommand(barbecuerCook);
            Command.Command fryDishCommand   = new FryDishCommand(fryDishCook);

            Waiter waiter = new Waiter();
            waiter.AddCommand(barbecuerCommand);
            waiter.AddCommand(fryDishCommand);
            waiter.Notify();

            #endregion

            #region Adapter
            Console.WriteLine("------------Adapter------------------");
            BasketballPlayer        bp1 = new Forward();
            BasketballPlayer        bp2 = new Guard();
            ChineseCenter           cc  = new ChineseCenter();
            BasketballPlayer        bp3 = new CenterInterPreter(cc);
            List <BasketballPlayer> bps = new List <BasketballPlayer>();
            bps.Add(bp1);
            bps.Add(bp2);
            bps.Add(bp3);
            foreach (var item in bps)
            {
                item.Attack();
                item.Defense();
            }
            #endregion

            #region Facade
            Console.WriteLine("------------Facade------------------");
            FundFacade ff = new FundFacade();
            ff.Buy();
            ff.Sell();
            #endregion


            #region Bridge
            {
                Console.WriteLine("------------Bridge---------------------");
                IProtocol     udp = new Bridge.Udp();
                IProtocol     tcp = new Bridge.Tcp();
                Communication c   = new Communication();
                c.SetProtocol(udp);
                c.Work();
                c.SetProtocol(tcp);
                c.Work();
            }
            #endregion

            #region Template
            {
                Console.WriteLine("------------Template-----------------------");
                MediaProtocol tcp = new Template.Tcp();
                tcp.Work();
                MediaProtocol udp = new Template.Udp();
                udp.Work();
            }

            #endregion

            #region Composite
            {
                Console.WriteLine("------------Composite-----------------------");
                FileComponent First = new FileFolder()
                {
                    Name = "C"
                };
                FileComponent second1 = new FileFolder()
                {
                    Name = "-Folder1"
                };
                FileComponent second2 = new File()
                {
                    Name = "-File2"
                };
                FileComponent second1_1 = new File()
                {
                    Name = "--File1_1"
                };
                second1.Add(second1_1);
                First.Add(second1);
                First.Add(second2);
                First.Display();
            }
            #endregion

            Console.ReadLine();
        }
Example #17
0
        static void Main(string[] args)
        {
            Console.WriteLine("Without Rx");
            Console.WriteLine("======");

            var iterable = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            foreach (var iterator in iterable)
            {
                if (iterator < 10)
                {
                    System.Console.WriteLine(iterator);
                }
            }


            Console.WriteLine("");
            Console.WriteLine("With LINQ");
            Console.WriteLine("======");
            var query = iterable.Where(iterator => iterator < 10).Select(iterator => { System.Console.WriteLine(iterator); return(iterator); });

            query.ToList();


            Console.WriteLine("");
            Console.WriteLine("With ProtoRx");
            Console.WriteLine("======");

            var ApplesStock = new StockObservable {
                Name = "Apples stock"
            };
            var PearsStock = new StockObservable {
                Name = "Pears stock"
            };
            var CabinetsStock = new StockObservable {
                Name = "Cabinets stock"
            };

            var FruitStore = new StockObserver {
                Name = "Fruit store"
            };
            var Storage = new StockObserver {
                Name = "storage"
            };

            ApplesStock.Subscribe(FruitStore);
            ApplesStock.Subscribe(Storage);

            PearsStock.Subscribe(FruitStore);
            PearsStock.Subscribe(Storage);

            CabinetsStock.Subscribe(Storage);

            Console.WriteLine("");
            Console.WriteLine("Initialize apples and pears to 20 units");
            ApplesStock.Units = 20;
            PearsStock.Units  = 20;

            Console.WriteLine("");
            for (int i = 0; i < 5; i++)
            {
                if (i % 2 == 0)
                {
                    Console.WriteLine("Rest an apple");
                    ApplesStock.Units--;
                }
                else
                {
                    Console.WriteLine("Rest a pear");
                    PearsStock.Units--;
                }

                Thread.Sleep(1000);
            }

            Console.WriteLine("");
            Console.WriteLine("Initialize cabinets to 3 units");
            CabinetsStock.Units = 3;

            Thread.Sleep(1000);

            Console.WriteLine("Add a cabinet");
            CabinetsStock.Units = 4;

            System.Console.ReadLine();
        }