Example #1
0
        static void Main(string[] args)
        {
            //Make a car object
            Car car1 = new Car("SlugBug", 100, 10);

            //now tell the car which method to call
            //when it wants to send a message
            car1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            car1.RegisterWithCarEngine(handler2);

            //speed up this will trigger the event
            Console.WriteLine("**** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                car1.Accelerate(20);
            }

            //unregister from the second handler
            car1.UnRegisterWithCarEngine(handler2);

            //We wont see the uppercase message anymore
            //for (int i = 0; i < 6; i++)
            //    car1.Accelerate(20);

            Console.ReadKey();
        }
Example #2
0
        static void Main( string[] args )
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the 'uppercase' message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();
        }
Example #3
0
        static void Main(string[] args)
        {
            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEnginHandler(OnCarEngineEvent));
            //c1.RegisterWithCarEngine(new Car.CarEnginHandler(OnCarEngineEvent2));

            //先绑定委托对象,稍后再注销.和上面不同的是,这里创建了这个委托类型的对象,并保存在本地变量中,这样就可以使用该对象在后面注销通知
            Car.CarEnginHandler handler2 = new Car.CarEnginHandler(OnCarEngineEvent2);

            c1.RegisterWithCarEngine(handler2);

            Console.WriteLine("***** Speed Up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
                //Console.ReadLine();
            }
            //注销第二个处理程序
            c1.UnRegisterWithCarEngine(handler2);


            //看不到大写消息了
            Console.WriteLine("***** Speed Up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
                //Console.ReadLine();
            }

            Console.ReadLine();
        }
Example #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // Создать объект Car.
            Car car = new Car("Ford", 50, 100);

            // Сообщить объекту Car, какой метод вызывать,
            // когда он пожелает отправить сообщение.
            //car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent1));
            //car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            Car.CarEngineHandler handler1 = new Car.CarEngineHandler(OnCarEngineEvent1);
            car.RegisterWithCarEngine(handler1);
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            car.RegisterWithCarEngine(handler2);

            // Увеличить скорость (это инициирует события).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 5; i++)
            {
                car.Accelerate(10);
            }

            car.UnRegisterWithCarEngine(handler2);
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 5; i++)
            {
                car.Accelerate(10);
            }

            Console.ReadLine();
        }
Example #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Разгоняемся (порождая события).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Отключаеи второй обработчик
            c1.UnRegisterWithCarEngine(handler2);

            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Example #6
0
        static void RemoveTargets()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("=> Removing Targets from a Delegate's Invocation List");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            Console.WriteLine("===== Speeding up =====");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);
            c1.Speed = 10;
            c1.StatusReset();
            Console.WriteLine("----- Speeding up -----");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }
        }
Example #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            //  首先,创建一个Car对象
            Car c1 = new Car("SlugBug", 100, 10);

            //  现在,告诉汽车,他想要向我们发送信息时调用哪个方法
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //  先绑定委托对象,稍后再注销
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            //  加速 (这将触发事件)
            Console.WriteLine("***** Speeding Up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            //  注销第二个处理程序
            c1.UnRegisterWithCarEngine(handler2);

            //  看不到大写的消息了
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Example #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            // Создать объект Car.
            Car car = new Car("Lada", 100, 10);

            // Сообщить объекту Car, какой метод вызывать,
            // когда он пожелает отправить сообщение.

            car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent1));
            car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            // Увеличить скорость (это инициирует события).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                car.Accelerate(20);
            }
            //Удаляет функцию из списка
            car.UnRegisterWithCarEngine(OnCarEngineEvent2);

            for (int i = 0; i < 6; i++)
            {
                car.Accelerate(20);
            }

            Console.ReadLine();
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            var c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            //c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            var handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);

            Console.WriteLine("***** Speeding up *****");
            for (var i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);

            Console.WriteLine("***** Speeding up *****");
            for (var i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Example #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            // This time, hold onto the delegate object,
            // so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);
            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
        }
Example #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call
            // when it wants to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // Register multiple targets for the notifications.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);


            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up Again*****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine("***** Method Group Conversion *****\n");
            Car c2 = new Car();

            // Register the simple method name.
            c2.RegisterWithCarEngine(OnCarEngineEvent);

            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }

            // Unregister the simple method name.
            c2.UnRegisterWithCarEngine(OnCarEngineEvent);

            // No more notifications!
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }


            Console.ReadLine();
        }
Example #12
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine("***** Agh! No Encapsulation! *****\n");
            // Make a Car.
            Car myCar = new Car();

            // We have direct access to the delegate!
            myCar.listOfHandlers = new Car.CarEngineHandler(CallWhenExploded);
            myCar.Accelerate(10);

            // We can now assign to a whole new object...
            // confusing at best.
            myCar.listOfHandlers = new Car.CarEngineHandler(CallHereToo);
            myCar.Accelerate(10);

            // The caller can also directly invoke the delegate!
            myCar.listOfHandlers.Invoke("hee, hee, hee...");

            Console.ReadLine();
        }
Example #13
0
        static void Main(string[] args)
        {
            // Console.WriteLine("***** Delegates as event enablers *****\n");
            //
            // Car c1 = new Car("Slugbug", 100, 10);
            // c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            //
            // Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            // c1.RegisterWithCarEngine(handler2);
            //
            // Console.WriteLine("***** Speeding up *****");
            // for (int i = 0; i < 6; i++)
            // {
            //     c1.Accelerate(20);
            // }
            //
            // c1.UnRegisterWithCarEngine(handler2);
            //
            // Console.WriteLine("***** Speeding up *****");
            // for (int i = 0; i < 6; i++)
            // {
            //     c1.Accelerate(20);
            // }

            Console.WriteLine("***** Method Group Conversion *****\n");
            Car c2 = new Car();

            c2.RegisterWithCarEngine(OnCarEngineEvent);

            Console.WriteLine("***** Speeding Up *****");
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }

            c2.UnRegisterWithCarEngine(OnCarEngineEvent);

            // No more notifications!
            Console.WriteLine("No more notifications!");
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enables *****\n");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler engineHandler = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            c1.UnRegisterWithCarEngine(engineHandler);
            Console.WriteLine("******Speeding up!********");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
        }
Example #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Delegated as event enablers ****");
            Car c1 = new Car("Jimmy", 0, 110);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler handler = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler);
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler);
            c1.Accelerate(10);

            Console.ReadLine();
        }
Example #16
0
        static void Main(string[] args) {
            Car c1 = new Car("SlugBug", 100, 10);
            //c1.RegisterWithCarEngine(OnCarEngineEvent);          Also works fine, method group conversion
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            for (int i = 0; i < 6; i++) {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);
            for (int i = 0; i < 2; i++) {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Example #17
0
        static void Main(string[] args)
        {
            // First, make a Car Object
            Car c1 = new Car("SlougBug", 100, 10);

            // Now, tell the car which method to call
            // when it wants to send us messages.
            // Extra-register multiple targets for the notifications
            c1.RegisterWithCarEngine(new Car.CarEngineHanlder(OnCarEngineEvent));
            // c1 i                           => the event source object
            // .RegisterWithCarEngine         => provides the registration/helper function
            // new Car.CarEngineHandler(...   => Listener Object                                            (EVENT HANDLER)
            // OnCarEngineEvent               => Event Handler Method / Listner Handler Object's method     (EVENT HANDLER)

            Car.CarEngineHanlder handler2 = new Car.CarEngineHanlder(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("**** Speeding up ****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("**** Speeding up ****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            //--------------------------Water Bottle---------------------------------------//
            PlasticWaterBottle myBottle = new PlasticWaterBottle(aName:"DANASI");
            //myBottle.RegisterWithBottleOfWater(new PlasticWaterBottle.WaterNotification(MartinBottle)); // OLD WAY
            myBottle.RegisterSubscribers(MartinBottle01); // NEW WAY

            // Lets fill the water
            for (int i = 0; i < 20; i++) {
                myBottle.fillWater(i);
            }

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car myCar = new Car("SlugBug", 100, 10);

            // register the handler, note anonymous class. Note the arg as a handler
            // that conforms to the delegate method signature
            // If you change the method handler arguments below, you will get a compile time error.

            myCar.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later.
            // The Register function keeps a list of all the event handlers
            //  that conform to the delegate signature.

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            myCar.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                myCar.Accelerate(20);
            }

            // Unregister from the second handler.
            myCar.UnRegisterWithCarEngine(handler2);

            // We won't see the 'uppercase' message anymore!
            Console.WriteLine("***** Speeding up  but unregistered 2nd event handler*****");
            for (int i = 0; i < 6; i++)
            {
                myCar.Accelerate(20);
            }

            Console.ReadLine();
        }
Example #19
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            //make a car
            Car c1 = new Car("SlugBug", 100, 10);
            //now tell the car which method(s) to call
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //we want an object that we can subsequently UNREGISTER...
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            //UNREGISTER the second handler
            c1.UnRegisterWithCarEngine(handler2);//enabling this line will UNREGISTER the second
            //event

            //Speed up (this will trigger events)
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();
        }