Esempio n. 1
0
        public static void TestChainOfResp()
        {
            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)
            {
                var handler = h1.HandleRequest(request, true);
                if (request >= 0 && request < 10)
                {
                    Assert.AreEqual(h1, handler);
                }
                else if (request >= 10 && request < 20)
                {
                    Assert.AreEqual(h2, handler);
                }
                else
                {
                    Assert.AreEqual(h3, handler);
                }
            }
        }
        public void Show()
        {
            var reqs = new List <int>
            {
                2,
                7,
                23,
                34,
                4,
                5,
                8,
                3
            };

            var h1 = new ConcreteHandler1(3);
            var h2 = new ConcreteHandler2(7);
            var h3 = new ConcreteHandler3(20);

            h1.SetHandler(h2);
            h2.SetHandler(h3);

            foreach (var req in reqs)
            {
                h1.Operation("operation is fired!", req);
            }
        }
Esempio n. 3
0
        static void StructuralExampleMain()
        {
            Console.WriteLine("Structural example");

            Handler handler1 = new ConcreteHandler1();
            Handler handler2 = new ConcreteHandler2();
            Handler handler3 = new ConcreteHandler3();

            handler1.SetSuccessor(handler2);
            handler2.SetSuccessor(handler3);

            handler1.HandleRequest();

            Console.WriteLine("Real world example");

            PoSystem poSystem = new PoSystem();

            Console.WriteLine("Handle a purchase of 5000");
            poSystem.ProcessRequest(5000);
            Console.WriteLine("Handle a purchase of 15000");
            poSystem.ProcessRequest(15000);
            Console.WriteLine("Handle a purchase of 70000");
            poSystem.ProcessRequest(70000);
            Console.WriteLine("Handle a purchase of 150000");
            poSystem.ProcessRequest(150000);
        }
Esempio n. 4
0
        void Main()
        {
            Handler h1 = new ConcreteHandler1();
            Handler h2 = new ConcreteHandler2();
            Handler h3 = new ConcreteHandler3();

            h1.Successor = h2;
            h2.Successor = h3;
            h1.HandleRequest(2);
        }
Esempio n. 5
0
        public static void Test()
        {
            Handler concreteHandler1 = new ConcreteHandler1();
            Handler concreteHandler2 = new ConcreteHandler2();
            Handler concreteHandler3 = new ConcreteHandler3();

            concreteHandler1.Successor = concreteHandler2;
            concreteHandler2.Successor = concreteHandler3;

            concreteHandler1.ProcessRequest(new object());
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            AbstractHandler handler1 = new ConcreteHandler1();
            AbstractHandler handler2 = new ConcreteHandler2();
            AbstractHandler handler3 = new ConcreteHandler3();

            handler1.Succsesor = handler2; //1->2
            handler2.Succsesor = handler3; //2->3
                                           //3-> ...
            handler1.RequestHandler(3);    // request for handler3 // tree
        }
Esempio n. 7
0
    public static void UnitTest()
    {
        ConcreteHandler3 theHandler3 = new ConcreteHandler3(null);
        ConcreteHandler2 theHandler2 = new ConcreteHandler2(theHandler3);
        ConcreteHandler1 theHandler1 = new ConcreteHandler1(theHandler2);

        theHandler1.HandleRequest(10);
        theHandler1.HandleRequest(15);
        theHandler1.HandleRequest(20);
        theHandler1.HandleRequest(30);
        theHandler1.HandleRequest(100);
    }
    //
    void UnitTest()
    {
        // 建立Cost验证的连接方式
        ConcreteHandler3 theHandle3 = new ConcreteHandler3(null);
        ConcreteHandler2 theHandle2 = new ConcreteHandler2(theHandle3);
        ConcreteHandler1 theHandle1 = new ConcreteHandler1(theHandle2);

        // 确认
        theHandle1.HandleRequest(10);
        theHandle1.HandleRequest(15);
        theHandle1.HandleRequest(20);
        theHandle1.HandleRequest(30);
        theHandle1.HandleRequest(100);
    }
Esempio n. 9
0
        public void ChainOfResponsibilityUsage()
        {
            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);
            }
        }
Esempio n. 10
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();
        }
	// 
	void UnitTest () {

		// 建立Cost驗証的連接方式
		ConcreteHandler3 theHandle3 = new ConcreteHandler3(null);
		ConcreteHandler2 theHandle2 = new ConcreteHandler2(theHandle3);
		ConcreteHandler1 theHandle1 = new ConcreteHandler1(theHandle2);

		// 確認
		theHandle1.HandleRequest(10);
		theHandle1.HandleRequest(15);
		theHandle1.HandleRequest(20);
		theHandle1.HandleRequest(30);
		theHandle1.HandleRequest(100);

	
	}
Esempio n. 12
0
        public void StructuralTest()
        {
            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);
            }
        }
        public void TestChainOfResponsibility(int request)
        {
            var handler1 = new ConcreteHandler1();
            var handler2 = new ConcreteHandler2();
            var handler3 = new ConcreteHandler3();

            handler1.Successor = handler2;
            handler2.Successor = handler3;

            handler1.HandleRequest(request);

            // OUTPUT: ConcreteHandler1 handled request: 1.

            // OUTPUT: ConcreteHandler2 handled request: 12.

            // OUTPUT: ConcreteHandler3 handled request: 23.
        }
Esempio n. 14
0
    void Start()
    {
        // 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);
        }
    }
Esempio n. 15
0
        static void Main(string[] args)
        {
            //职责链模式

            Handler handler1 = new ConcreteHandler1();
            Handler handler2 = new ConcreteHandler2();
            Handler handler3 = new ConcreteHandler3();

            handler1.SetSuccessor(handler2);
            handler2.SetSuccessor(handler3);

            int[] request = { 2, 6, 10, 15, 18, 20, 28, 29, 8, 5, 4 };

            foreach (int item in request)
            {
                handler1.HandleRequest(item);
            }
            Console.WriteLine("*****************************************");



            CommonManager jingli     = new CommonManager("金立");
            Majordomo     zongjian   = new Majordomo("张无忌");
            GeneraManager zongjingli = new GeneraManager("张三丰");

            jingli.SetSuperior(zongjian);
            zongjian.SetSuperior(zongjingli);

            jingli.RequestApploactions(new Request {
                Number = 1, RequestType = "请假", RequestContent = "请假一天"
            });
            jingli.RequestApploactions(new Request {
                Number = 10, RequestType = "请假", RequestContent = "请假一天"
            });

            jingli.RequestApploactions(new Request {
                Number = 500, RequestType = "加薪", RequestContent = "请求加薪"
            });

            jingli.RequestApploactions(new Request {
                Number = 1000, RequestType = "加薪", RequestContent = "请求加薪"
            });

            Console.Read();
        }
Esempio n. 16
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);
            }
            // Wait for user
            Console.Read();
        }
    static void Main()
    {
        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();
    }
Esempio n. 18
0
        public UsingExample()
        {
            // 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();
        }
Esempio n. 19
0
        private static void callChainOfResponsibility()
        {
            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();
        }
Esempio n. 20
0
        static void ChainOfResponsibilityTester()
        {
            #region sample 1
            // 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 sample 2
            // Setup Chain of Responsibility
            Approver larry = new Director();
            Approver sam   = new VicePresident();
            Approver tammy = new President();

            larry.SetSuccessor(sam);
            sam.SetSuccessor(tammy);

            // Generate and process purchase requests
            Purchase p = new Purchase(2034, 350.00, "Assets");
            larry.ProcessRequest(p);

            p = new Purchase(2035, 32590.10, "Project X");
            larry.ProcessRequest(p);

            p = new Purchase(2036, 122100.00, "Project Y");
            larry.ProcessRequest(p);
            #endregion
        }
Esempio n. 21
0
    // Entry point into console application.
    static void Main()
    {
        // Setup Chain of Responsibility (top-down)
        Handler h4 = new ConcreteHandler4();
        Handler h3 = new ConcreteHandler3(h4);
        Handler h2 = new ConcreteHandler2();
        Handler h1 = new ConcreteHandler1();

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

        // Generate requests
        int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20, 3, 35, 50, 39, 42, 32 };

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

        // Wait for user
        Console.ReadKey();
    }
    // Entry point into console application.
    static void Main()
    {
        // Setup Chain of Responsibility (top-down)
        Handler h4 = new ConcreteHandler4();
        Handler h3 = new ConcreteHandler3(h4);
        Handler h2 = new ConcreteHandler2();
        Handler h1 = new ConcreteHandler1();

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

        // Generate requests
        int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20, 3, 35, 50, 39, 42, 32 };

        // Process requests
        foreach (int request in requests)
            h1.HandleRequest(request);

        // Wait for user
        Console.ReadKey();
    }
Esempio n. 23
0
        //static Memento memento;
        //static string path = @"D:\text.txt";
        static void Main(string[] args)
        {
            //var context = new Context();

            //var list = new List<AbstractExpression>();

            //list.Add(new TerminalExpression());
            //list.Add(new NonterminalExpression());
            //list.Add(new TerminalExpression());
            //list.Add(new TerminalExpression());

            //foreach (AbstractExpression exp in list)
            //{
            //    Console.WriteLine(exp.Interpret(context));
            //}

            //// MEMENTO
            //File.WriteAllText(path,"pierwszy tekst");
            //if (path != ""&& File.Exists(path))
            //{
            //    memento = new Memento(path, File.ReadAllBytes(path));
            //}
            //File.WriteAllText(path, "drugi tekst");
            //memento.RevertFromMemento();

            //File.WriteAllText(path, "trzeci tekst(do zapamietania)");
            //memento.SetNewData(path);
            //File.WriteAllText(path, "czwarty tekst");
            //memento.RevertFromMemento();

            //STATE
            //var small = new SmallState();
            //Contextt context = new Contextt(small);
            //Console.Read();
            //context.Change();
            //Console.Read();
            //context.Change();
            //Console.Read();
            //context.Change();

            ////STRATEGY
            //bool onceAgain = true;
            //while (onceAgain)
            //{
            //    Console.WriteLine("Wybierz postać: A,B,C");
            //    Console.WriteLine();
            //    ConsoleKeyInfo figure = Console.ReadKey(true);
            //    IFigure ifigure = null;
            //    switch (figure.Key)
            //    {
            //        case ConsoleKey.A:
            //            ifigure = new Figure1();
            //            break;
            //        case ConsoleKey.B:
            //            ifigure = new Figure2();
            //            break;
            //        case ConsoleKey.C:
            //            ifigure = new Figure3();
            //            break;
            //    }
            //    Console.WriteLine($"Wybrałes postać: { figure.Key}");
            //    Console.WriteLine(ifigure.Kopnięcie()[0]);
            //    Console.WriteLine(ifigure.Uderzenie()[0]);
            //    Console.WriteLine();
            //    onceAgain = true;
            //}
            //ITERATOR
            //ConcreteIterable concreteIterable = new ConcreteIterable();
            //concreteIterable[0] = 1;
            //concreteIterable[1] = 2;
            //concreteIterable[2] = 3;
            //IIterator iterator = concreteIterable.GetIterator();
            //Console.WriteLine(iterator.First());
            //while(!iterator.IsEnd())
            //{
            //    Console.WriteLine(iterator.Next());
            //}

            // OBSERVATOR
            //ConcreteSubject concreteSubject = new ConcreteSubject();
            //concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Pierwszy"));
            //concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Drugi"));
            //var third = new ConcreteObserver(concreteSubject, "trzeci");
            //concreteSubject.Attach(third);
            //concreteSubject.SubjectState = "Super stan";
            //concreteSubject.Notify();
            //concreteSubject.Detach(third);
            //concreteSubject.Notify();

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

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

            int[] request = { 1, 2, 3, 10, 11, 22, 25 };
            foreach (var item in request)
            {
                h3.HandleRequest(item);
            }



            Console.Read();
        }
Esempio n. 24
0
  public 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 );

    Console.Read();
  }