コード例 #1
0
        public static void ChainOfResponsibility()
        {
            //Create the chain links
            Approver jennifer = new HeadChef();
            Approver mitchell = new PurchasingManager();
            Approver olivia   = new GeneralManager();

            //Create the chain
            jennifer.SetSupervisor(mitchell);
            mitchell.SetSupervisor(olivia);

            // Generate and process purchase requests
            PurchaseOrder p = new PurchaseOrder(1, 20, 69, "Spices");

            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(2, 300, 1389, "Fresh Veggies");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(3, 500, 4823.99, "Beef");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(4, 4, 12099, "Ovens");
            jennifer.ProcessRequest(p);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: boogermanus/dotnet
        static void Client()
        {
            Approver jordan = new GeneralManager(null);
            Approver haley  = new PurchasingManager(jordan);
            Approver betsy  = new HeadChef(haley);

            PurchaseOrder po1 = new PurchaseOrder("Spices", 20, 3.50m);

            betsy.ProcessRequest(po1);
            Console.WriteLine(po1);

            PurchaseOrder po2 = new PurchaseOrder("Fresh Veggies", 300, 8);

            betsy.ProcessRequest(po2);
            Console.WriteLine(po2);

            PurchaseOrder po3 = new PurchaseOrder("Beef", 500, 9.23m);

            betsy.ProcessRequest(po3);
            Console.WriteLine(po3);

            PurchaseOrder po4 = new PurchaseOrder("Ovens", 5, 5000.21m);

            try
            {
                betsy.ProcessRequest(po4);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(po4);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: yordanov03/DesignPatterns
        static void Main(string[] args)
        {
            //Create the chain links

            Approver jennifer = new HeadChef();
            Approver mitchell = new PurchasingManager();
            Approver olivia   = new GeneralManager();

            //Create the chain
            jennifer.SetSupervisor(mitchell);
            mitchell.SetSupervisor(olivia);

            // Generate and process purchase requests
            PurchaseOrder p = new PurchaseOrder(1, 20, 69, "Spices");

            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(2, 300, 1389, "Fresh Veggies");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(3, 500, 4823.99, "Beef");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(4, 4, 12099, "Ovens");
            jennifer.ProcessRequest(p);

            // Wait for user
            Console.ReadKey();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: ajayvjain/DesignPatterns
        static void Main()
        {
            var headChef       = new HeadChef("Thiago");
            var kitchenManager = new KitchenManager("Jonathan");
            var generalManager = new GeneralManager("Yuliya");
            var restaurant     = new CORPizzaRestaurant.PizzaRestaurant(headChef, kitchenManager, generalManager);

            Console.WriteLine("===========================");
            Console.WriteLine("==      WELCOME TO       ==");
            Console.WriteLine("== Buona Sera Restaurant ==");
            Console.WriteLine("== Order Approval System ==");
            Console.WriteLine("===========================\n\n");

            Console.WriteLine("== Current hierarchy ==\n");
            foreach (var employee in restaurant.GetHierarchy())
            {
                Console.WriteLine($"{employee.Item1} as {employee.Item2.Name}");
            }

            Console.WriteLine("\n");

            Console.WriteLine("== Order Approvals ==\n");
            var ordersToApprove = new[] { 1000, 150, 250, 10, 5000, 450, 600, 75, 50 };
            var approvals       = restaurant.ProcessOrders(ordersToApprove);

            for (var orderNumber = 1; orderNumber <= ordersToApprove.Length; orderNumber++)
            {
                Console.WriteLine($"Order no {orderNumber}\nTotal cost: {ordersToApprove[orderNumber-1]}\n{approvals[orderNumber-1]}\n");
            }

            Console.ReadKey();
        }
コード例 #5
0
        public void SetSupervisor_WhenSetSupervisorIsCalled()
        {
            var headChef       = new HeadChef("Roberto");
            var kitchenManager = new KitchenManager("Rob");

            headChef.SetSupervisor(kitchenManager);

            headChef.Supervisor.Should().Be(kitchenManager);
        }
コード例 #6
0
        public void ReturnApprovedByHeadChef_WhenApproveOrderIsCalledWithTotalLessThan100()
        {
            var expectedApprover = "David";
            var expectedApproval = $"Approved by {expectedApprover}";
            var orderTotal       = 99;
            var headChef         = new HeadChef(expectedApprover);

            var approval = headChef.ApproveOrder(orderTotal);

            approval.Should().Be(expectedApproval);
        }
コード例 #7
0
        public void ReturnApprovedByKitchenManager_WhenApproveOrderIsCalledWithTotalBetween100And499()
        {
            var expectedApprover = "Adrian";
            var expectedApproval = $"Approved by {expectedApprover}";
            var orderTotal       = 499;
            var headChef         = new HeadChef("John");
            var kitchenMannager  = new KitchenManager(expectedApprover);

            headChef.SetSupervisor(kitchenMannager);

            var approval = headChef.ApproveOrder(orderTotal);

            approval.Should().Be(expectedApproval);
        }
コード例 #8
0
        public void ReturnRequiresBoardMeeting_WHenApproveOrderIsCalledWithTotalGreaterOrEqualThan2500()
        {
            var expectedApproval = "Requires Board Meeting";
            var orderTotal       = 2500;
            var headChef         = new HeadChef("Jonathan");
            var kitchenMannager  = new KitchenManager("Charlie");
            var generalManager   = new GeneralManager("Nick");

            headChef.SetSupervisor(kitchenMannager);
            kitchenMannager.SetSupervisor(generalManager);

            var approval = headChef.ApproveOrder(orderTotal);

            approval.Should().Be(expectedApproval);
        }
コード例 #9
0
        public void ReturnApprovedByGeneralManager_WhenApproveOrderIsCalledWithTotalBetween500And2499()
        {
            var expectedApprover = "Yuliya";
            var expectedApproval = $"Approved by {expectedApprover}";
            var orderTotal       = 2499;
            var headChef         = new HeadChef("Arun");
            var kitchenMannager  = new KitchenManager("Thiago");
            var generalManager   = new GeneralManager(expectedApprover);

            headChef.SetSupervisor(kitchenMannager);
            kitchenMannager.SetSupervisor(generalManager);

            var approval = headChef.ApproveOrder(orderTotal);

            approval.Should().Be(expectedApproval);
        }
コード例 #10
0
        public void ReturnExpectedApprovals_WhenProcessOrdersIsCalled()
        {
            var expectedApprovals = new List <string>
            {
                "Approved by Rob",
                "Approved by John",
                "Requires Board Meeting",
                "Approved by Roberto"
            };
            var orderTotals     = new[] { 1000, 150, 5000, 50 };
            var headChef        = new HeadChef("Roberto");
            var kitchenManager  = new KitchenManager("John");
            var generalManager  = new GeneralManager("Rob");
            var pizzaRestaurant = new PizzaRestaurant(headChef, kitchenManager, generalManager);

            var approvals = pizzaRestaurant.ProcessOrders(orderTotals);

            approvals.Should().BeEquivalentTo(expectedApprovals);
        }
コード例 #11
0
        public void ReturnFullRestaurantHierarchy_WhenGetHierarchyIsCalled()
        {
            var expectedHeadChef       = "Adrian";
            var expectedKitchenManager = "David";
            var expectedGeneralManager = "Yuliya";
            var expectedHierarchy      = new List <Tuple <string, Type> >
            {
                new Tuple <string, Type>(expectedHeadChef, typeof(HeadChef)),
                new Tuple <string, Type>(expectedKitchenManager, typeof(KitchenManager)),
                new Tuple <string, Type>(expectedGeneralManager, typeof(GeneralManager))
            };
            var headChef        = new HeadChef(expectedHeadChef);
            var kitchenManager  = new KitchenManager(expectedKitchenManager);
            var generalManager  = new GeneralManager(expectedGeneralManager);
            var pizzaRestaurant = new PizzaRestaurant(headChef, kitchenManager, generalManager);

            var restaurantHierarchy = pizzaRestaurant.GetHierarchy();

            restaurantHierarchy.Should().BeEquivalentTo(expectedHierarchy);
        }
コード例 #12
0
        static void Main(string[] args)
        {
            #region Publication

            // Create a list of books to be published, including cover type and publication cost.
            var books = new List <Book> {
                new Book("The Stand", "Stephen King", CoverType.Paperback, 35000),
                new Book("The Hobbit", "J.R.R. Tolkien", CoverType.Paperback, 25000),
                new Book("The Name of the Wind", "Patrick Rothfuss", CoverType.Digital, 7500),
                new Book("To Kill a Mockingbird", "Harper Lee", CoverType.Hard, 65000),
                new Book("1984", "George Orwell", CoverType.Paperback, 22500),
                new Book("Jane Eyre", "Charlotte Brontë", CoverType.Hard, 82750)
            };

            // Create specifications for individual cover types.
            var digitalCoverSpec   = new Specification <Book>(book => book.CoverType == CoverType.Digital);
            var hardCoverSpec      = new Specification <Book>(book => book.CoverType == CoverType.Hard);
            var paperbackCoverSpec = new Specification <Book>(book => book.CoverType == CoverType.Paperback);

            // Create budget spec for cost exceeding $75000.
            var extremeBudgetSpec = new Specification <Book>(book => book.PublicationCost >= 75000);
            // Create budget spec for cost between $50000 and $75000.
            var highBudgetSpec = new Specification <Book>(book => book.PublicationCost >= 50000 && book.PublicationCost < 75000);
            // Create budget spec for cost between $25000 and $50000.
            var mediumBudgetSpec = new Specification <Book>(book => book.PublicationCost >= 25000 && book.PublicationCost < 50000);
            // Create budget spec for cost below $25000.
            var lowBudgetSpec = new Specification <Book>(book => book.PublicationCost < 25000);

            // Default spec, always returns true.
            var defaultSpec = new Specification <Book>(book => true);

            // Create publication process instance, used to pass Action<T> to Employee instances.
            var publicationProcess = new PublicationProcess();

            // Create employees with various positions.
            var ceo       = new Employee <Book>("Alice", Position.CEO, publicationProcess.PublishBook);
            var president = new Employee <Book>("Bob", Position.President, publicationProcess.PublishBook);
            var cfo       = new Employee <Book>("Christine", Position.CFO, publicationProcess.PublishBook);
            var director  = new Employee <Book>("Dave", Position.DirectorOfPublishing, publicationProcess.PublishBook);
            // Default employee, used as successor of CEO and to handle unpublishable books.
            var defaultEmployee = new Employee <Book>("INVALID", Position.Default, publicationProcess.FailedPublication);

            // Director can handle digital low budget only.
            director.SetSpecification(digitalCoverSpec.And(lowBudgetSpec));

            // CFO can handle digital/paperbacks that are medium or high budget.
            cfo.SetSpecification(digitalCoverSpec.Or(paperbackCoverSpec).And(mediumBudgetSpec.Or(highBudgetSpec)));

            // President can handle all medium/high budget.
            president.SetSpecification(mediumBudgetSpec.Or(highBudgetSpec));

            // CEO can handle all extreme budget.
            ceo.SetSpecification(extremeBudgetSpec);

            // Default employee can handle only default specification (all).
            defaultEmployee.SetSpecification(defaultSpec);

            // Set chain of responsibility: CEO > President > CFO > Director.
            director.SetSuccessor(cfo);
            cfo.SetSuccessor(president);
            president.SetSuccessor(ceo);
            ceo.SetSuccessor(defaultEmployee);

            // Loop through books, trying to publish, starting at bottom of chain of responsibility (Director).
            books.ForEach(book => director.PublishBook(book));

            #endregion

            // TODO => Add specification pattern here.
            #region Restaurant

            //Create the chain links
            Approver jennifer = new HeadChef();
            Approver mitchell = new PurchasingManager();
            Approver olivia   = new GeneralManager();

            //Create the chain
            jennifer.SetSupervisor(mitchell);
            mitchell.SetSupervisor(olivia);

            // Generate and process purchase requests
            PurchaseOrder p = new PurchaseOrder(1, 20, 69, "Spices");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(2, 300, 1389, "Fresh Veggies");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(3, 500, 4823.99, "Beef");
            jennifer.ProcessRequest(p);

            p = new PurchaseOrder(4, 4, 12099, "Ovens");
            jennifer.ProcessRequest(p);

            #endregion

            // Wait for user
            Console.ReadKey();
        }