コード例 #1
0
ファイル: Program.cs プロジェクト: johndarv/CSharpExamples
        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
ファイル: Program.cs プロジェクト: johndarv/CSharpExamples
        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);
        }