示例#1
0
        private static void ReceiveInput(BigRedButton redButton, BigBlueButton blueButton)
        {
            string input = string.Empty;

            while (input != "quit" && input != "q")
            {
                input = Console.ReadLine();
                if (input == "push red")
                {
                    redButton.Push();
                }
                else if (input == "push blue")
                {
                    blueButton.Push();
                }
                else
                {
                    TypeIntructions();
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            BigMassiveRocket rocket = new BigMassiveRocket();

            BigRedButton redButton = new BigRedButton();
            BigBlueButton blueButton = new BigBlueButton();

            // Here we can subscribe to the OnButtonPush event from outside of the class, because the event is public.
            // HOWEVER we do not have permissions to call redButton.OnButtonPush() to invoke all subscribers.
            // The ability to invoke like this is nicely hidden away within the class.
            redButton.OnButtonPush += rocket.Launch;

            // Here we have had to use a RegisterHandler method to separate the private delegate away from anything outside of the class.
            // If we had made blueButton.onButtonPush public, then we would be able to invoke it with blueButton.OnButtonPush().
            // This is the main difference between events and delegates. Events provide a nice clean way of hiding the ability to invoke
            // from anything outside of the "publisher" class. This is similar to the way that properties differ from fields.
            blueButton.RegisterHandler(rocket.Launch);

            Console.WriteLine("You are in a dark room. There is a big red button and a big blue button.");

            TypeIntructions();

            ReceiveInput(redButton, blueButton);
        }