コード例 #1
0
        private static void Main(string[] args)
        {
            #region 结构实现

            // Setup Chain of Responsibility
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Generate and process request
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }

            #endregion

            Console.WriteLine("******************************");

            #region 实践应用

            #endregion

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            /*
             * More info: https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
             * http://www.dofactory.com/net/chain-of-responsibility-design-pattern
             * What problems can the Chain of Responsibility design pattern solve?
             * Coupling the sender of a request to its receiver should be avoided.
             * It should be possible that more than one receiver can handle a request.
             *
             * Implementing a request directly within the class that sends the request is inflexible because it couples the class to a particular receiver and makes it impossible
             * to support multiple receivers.
             */


            // Setup Chain of Responsibility
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Generate and process request
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }

            Console.ReadKey();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            // Setup Chain of Responsibility

            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Generate and process request

            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }

            //throw new ArgumentOutOfRangeException();
            // Wait for user

            Console.ReadKey();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }
            Console.ReadKey();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            #region GoF

            // Setup Chain of Responsibility
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();
            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Generate and process request
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }

            #endregion

            #region Logger

            // Build the chain of responsibility
            Logger logger, logger1, logger2;
            logger  = new ConsoleLogger(LogLevel.All);
            logger1 = logger.SetNext(new EmailLogger(LogLevel.FunctionalMessage | LogLevel.FunctionalError));
            logger2 = logger1.SetNext(new FileLogger(LogLevel.Warning | LogLevel.Error));

            // Handled by ConsoleLogger since the console has a loglevel of all
            logger.Message("Entering function ProcessOrder().", LogLevel.Debug);
            logger.Message("Order record retrieved.", LogLevel.Info);

            // Handled by ConsoleLogger and FileLogger since filelogger implements Warning & Error
            logger.Message("Customer Address details missing in Branch DataBase.", LogLevel.Warning);
            logger.Message("Customer Address details missing in Organization DataBase.", LogLevel.Error);

            // Handled by ConsoleLogger and EmailLogger as it implements functional error
            logger.Message("Unable to Process Order ORD1 Dated D1 For Customer C1.", LogLevel.FunctionalError);

            // Handled by ConsoleLogger and EmailLogger
            logger.Message("Order Dispatched.", LogLevel.FunctionalMessage);

            #endregion

            // Wait for user
            Console.ReadKey();
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            // Structure
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();

            h1.SetSuccessor(h2);
            int[] request = { 3, 4, 10, 12, 3, 7, 13 };

            foreach (int i in request)
            {
                h1.HandleRequest(i);
            }

            Console.Read();
        }
コード例 #7
0
        public static void Main()
        {
            IHandler h1 = new ConcreteHandler1();
            IHandler h2 = new ConcreteHandler2();
            IHandler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }
        }
コード例 #8
0
        static void Main(string[] args)
        {
            // canonical
            var h1 = new ConcreteHandler1();
            var h2 = new ConcreteHandler2();

            h1.SetSuccessor(h2);

            int[] requests = { 1, 5, 10, 50, 100 }; // 100 not handled at all
            foreach (var request in requests)
            {
                h1.HandleRequest(request);
            }

            Console.WriteLine();
        }
コード例 #9
0
        static void Main()
        {
            // Запуск цепочки обязанностей
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Генерация запросов
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }

            Console.ReadKey();
        }
コード例 #10
0
        static void Main()
        {
            // Setup Chain of Responsibility
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Generate and process request
            int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

            foreach (var request in requests)
            {
                h1.HandleRequest(request);
            }

            // Wait for user
            Console.Read();
        }
コード例 #11
0
        static void Main(string[] args)
        {
            // Setup Chain of Responsibility
            AbstractHandler h1 = new ConcreteHandler1();
            AbstractHandler h2 = new ConcreteHandler2();
            AbstractHandler h3 = new ConcreteHandler3();

            h1.SetSuccessor(h2);
            h2.SetSuccessor(h3);

            // Generate and process request
            int[] requests = { 20, 5, 0, -5, 220, 18, 7 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }

            // Wait for user
            Console.ReadKey();
        }
コード例 #12
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // Setup Chain of Responsibility

                Handler h1 = new ConcreteHandler1();

                Handler h2 = new ConcreteHandler2();

                Handler h3 = new ConcreteHandler3();

                h1.SetSuccessor(h2);

                h2.SetSuccessor(h3);

                // Generate and process request

                int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

                foreach (int request in requests)
                {
                    h1.HandleRequest(request);
                }

                // Wait for user

                Console.ReadKey();
        }