コード例 #1
0
ファイル: Program.cs プロジェクト: mjquito/sandbox_csharp
        static void Main(string[] args)
        {
            Car c1 = new Car("SlugBug", 100, 10);

            // Register event handlers.
            c1.AboutToBlow += delegate {
                Console.WriteLine("It is doomed!!!");
            };

            c1.AboutToBlow += new Car.CarEngineHanlder(CarIsAboutToBlow);

            Car.CarEngineHanlder d = new Car.CarEngineHanlder(CarExploded);
            c1.Exploded += d;

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

            // Remove CarExploded method
            // from invoation list
            c1.Exploded -= d;

            Console.WriteLine("\n*****Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: mjquito/sandbox_csharp
        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();
        }