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)
        {
            /*
             * 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 #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();
        }
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");
        }
        static void Main(string[] args)
        {
            IHandler handler = new ConcreteHandler1();
            handler.Successor = new ConcreteHandler2();

            handler.HandlerRequest("Question:What are you doing?");
            handler.HandlerRequest("Answer:Coding all the night.");

            Console.Read();
        }
Example #8
0
		static void Main(string[] args) {
			Handler handlerChain = new ConcreteHandler1(new ConcreteHandler2(null));

			Request request1 = new ConcreteRequest1();
			handlerChain.HandleRequest(request1);

			Request request2 = new ConcreteRequest2();
			handlerChain.HandleRequest(request2);

			Console.ReadKey();
		}
Example #9
0
        static void Main(string[] args)
        {
            ConcreteHandler1 ch1 = new ConcreteHandler1();
            ConcreteHanlder2 ch2 = new ConcreteHanlder2();

            ch1.Successor = ch2;

            ch1.HandleRequest(3);
            ch1.HandleRequest(18);
            ch1.HandleRequest(25);
        }
        static void Main(string[] args)
        {
            Handler handlerChain = new ConcreteHandler1(new ConcreteHandler2(null));

            Request request1 = new ConcreteRequest1();

            handlerChain.HandleRequest(request1);

            Request request2 = new ConcreteRequest2();

            handlerChain.HandleRequest(request2);

            Console.ReadKey();
        }
Example #11
0
        static void Main(string[] args)
        {
            ConcreteHandler1 mainHandler = new ConcreteHandler1();
            mainHandler.Successor = new ConcreteHandler2();
            mainHandler.Successor.Successor = new ConcreteHandler3();

            int[] requests = { 0, 5, 9, 11, 18, 25, 29, 34};

            foreach (var item in requests)
            {
                mainHandler.HandleRequest(item);
            }

            Console.ReadLine();
        }
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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);
            }
        }
        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 #21
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 #22
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 #23
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 #24
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();
        }