Example #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();
        }
Example #2
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();
        }
Example #3
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();
        }
Example #4
0
        static void Main(string[] args)
        {        //声明出所有的处理节点
            Handler handler1 = new ConcreteHandler1();
            Handler handler2 = new ConcreteHandler2();
            Handler handler3 = new ConcreteHandler3();

            //设置链中的阶段顺序,1-->2-->3
            handler1.setNext(handler2);
            handler2.setNext(handler3);
            //提交请求,返回结果
            Response response = handler1.handlerMessage(new Request(new Level()
            {
                num = 2
            }));

            Console.WriteLine(response);


            Demo.Handler PM = new Demo.PM();
            Demo.Handler DM = new Demo.DM();
            Demo.Handler VP = new Demo.VP();
            //设置链中的阶段顺序,1-->2-->3
            PM.setNext(DM);
            DM.setNext(VP);
            //提交请求,返回结果
            Demo.Response Response1 = PM.handlerMessage(new Demo.Request(1));
            Demo.Response Response2 = PM.handlerMessage(new Demo.Request(2));
            Demo.Response Response3 = PM.handlerMessage(new Demo.Request(3));

            Console.ReadLine();
        }
Example #5
0
        static void Main(string[] args)
        {
            ConcreteHandler1 handler1 = new ConcreteHandler1();
            ConcreteHandler2 handler2 = new ConcreteHandler2();

            handler1.successor = handler2;
            handler1.HandleRequest(1);
            handler1.HandleRequest(2);
        }
Example #6
0
        static void Main(string[] args)
        {
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();

            h1.Successor = h2;
            //h1.HandleRequest("One");
            h1.HandleRequest("Two");
        }
Example #7
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();
        }
Example #8
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();
        }
Example #9
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);
            }
        }
Example #10
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();
        }
Example #11
0
        static void Main(string[] args)
        {
            var h1 = new ConcreteHandler1();
            var h2 = new ConcreteHandler2();
            var h3 = new ConcreteHandler3();


            h1.SetSucessor(h2);
            h2.SetSucessor(h3);

            int[] requests = { 200, 5, 24, 22, 18, 3, 27, 20 };
            foreach (var request in requests)
            {
                h1.HandleRequest(request);
            }
        }
Example #12
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();
        }
Example #13
0
        //maneira de passar uma requisicao por meio de uma cadeia de objetos
        static void Main(string[] args)
        {
            Handler H1 = new ConcreteHandler1();
            Handler H2 = new ConcreteHandler2();
            Handler H3 = new ConcreteHandler3();

            //cadeia de objetos
            H1.SetSucessor(H2);
            H2.SetSucessor(H3);

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

            foreach (int request in requests)
            {
                H1.HandleRequest(request);
            }
        }
Example #14
0
        static void Main(string[] args)
        {
            Handler h1 = new ConcreteHandler1();

            Handler h2 = new ConcreteHandler2();

            Handler h3 = new ConcreteHandler3();

            h1.sucessor = h2;
            h2.sucessor = h3;

            int[] requests = { 2, 5, 24, 22, 1, 30, 12 };

            foreach (int request in requests)
            {
                h1.HandleRequest(request);
            }
        }
Example #15
0
        static void Main(string[] args)
        {
            Handler h1 = new ConcreteHandler();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.SetSucessor(h2);
            h2.SetSucessor(h3);

            var requests = new[] { 1, 2, 35, 67, 23, 78, 43 };

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

            Console.ReadLine();
        }
        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();
        }
Example #17
0
        static void Main(string[] args)
        {
            var h1 = new ConcreteHandler1();
            var h2 = new ConcreteHandler2(h1);
            var h3 = new ConcreteHandler3(h2);

            h3.HandleRequest(8);
            h3.HandleRequest(12);
            h3.HandleRequest(3);
            h3.HandleRequest(24);
            h3.HandleRequest(19);
            h3.HandleRequest(33);
            h3.HandleRequest(17);
            h3.HandleRequest(1);
            h3.HandleRequest(15);
            h3.HandleRequest(28);


            Console.ReadKey();
        }
Example #18
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();
        }
Example #19
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();
        }
Example #20
0
        static void Main(string[] args)
        {
            //有若干任务处理者
            AbstractHandler hander1 = new ConcreteHandler1();
            AbstractHandler hander2 = new ConcreteHandler2();
            AbstractHandler hander3 = new ConcreteHandler3();


            //根据需要,将任务处理者,动态的组成一个链表(可以在其他地方进一步封装)
            hander1.SettHandler(hander2);
            hander2.SettHandler(hander3);

            //  hander3.SettHandler(hander8);//实际中,可以增加很多的任务处理者,而且可以动态组合链表
            hander1.RequstAction(11);

            //客户端使用
            hander1.RequstAction(2);
            hander1.RequstAction(6);
            hander1.RequstAction(7);

            Console.Read();
        }
        /// <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();
        }