コード例 #1
0
        static void Main(string[] args)
        {
            var addCustomerCommand = new AddCustomerCommand(new CustomerService());
            var button             = new Button(addCustomerCommand);

            button.Click();

            // Because we are representing each individual task using a command object,
            // we can combine these commands inside a composite object and re-execute them later on.
            var compositeCommand = new CompositeCommand();

            compositeCommand.Add(new ResizeCommand());
            compositeCommand.Add(new BlackAndWhiteCommand());

            compositeCommand.Execute();

            var history      = new Solution.UndoableCommands.History();
            var htmlDocument = new HtmlDocument();

            htmlDocument.Content = "Hello World!";

            var boldCommand = new BoldCommand(htmlDocument, history);

            boldCommand.Execute();

            Console.WriteLine(htmlDocument.Content);

            //boldCommand.Unexecute();
            var undoCommand = new Solution.UndoableCommands.UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(htmlDocument.Content);

            Console.WriteLine("--- Exercise ---");

            var exeHistory  = new Exercise.End.History();
            var videoEditor = new VideoEditor();

            var setTextCommand = new SetTextCommand("Hello World!", videoEditor, exeHistory);

            setTextCommand.Execute();

            Console.WriteLine(videoEditor.Text);

            //setTextCommand.Undo();
            var exeUndoCommand = new Exercise.End.UndoCommand(exeHistory);

            exeUndoCommand.Execute();

            Console.WriteLine(videoEditor.Text);

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var invoiceService   = new InvoiceService();
            var inventoryService = new InventoryService();
            var history          = new CommandHistory();

            var products = new List <Product>
            {
                new Product {
                    Name = "Soap", Price = 10.00m
                },
                new Product {
                    Name = "Juice", Price = 5.99m
                },
                new Product {
                    Name = "PaperTowel", Price = 2.99m
                },
                new Product {
                    Name = "Yogurt", Price = 4.99m
                },
            };
            var command = new CompositeCommand();

            command.Add(new AddProductCommand(products[0], invoiceService, inventoryService, history));
            command.Add(new AddProductCommand(products[1], invoiceService, inventoryService, history));

            // Option 1 - If a command is undoable then we can just add undo action to revert back
            command.Add(new AddProductCommand(products[3], invoiceService, inventoryService, history));
            command.Add(new UndoCommand(history)); // First undo to remove last product added
            command.Add(new UndoCommand(history)); // Second undo to remove last but one product added

            // Option 2 - If a command is not undoable then we can have explicit remove command to revert the changes
            command.Add(new AddProductCommand(products[2], invoiceService, inventoryService, history));
            command.Add(new RemoveProductCommand(products[2], invoiceService, inventoryService));

            var cart = new Cart(command);

            cart.OneClickBuy();

            System.Console.WriteLine($"You purchased {invoiceService.TotalCountofProducts} item/s and your total price is {invoiceService.TotalPrice}");
            System.Console.WriteLine($"The {inventoryService.TotalCountofProducts} item/s purchased will be delivered on {inventoryService.DeliveryDate}");
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: psym0n76/DesignPatterns
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // decoupling the customer service from the button
            // otherwise all code has to be retained in the button

            var service = new CustomerService();
            var command = new AddCustomerCommand(service);
            var button  = new Button(command);

            button.Click();

            var composite = new CompositeCommand();

            composite.Add(new ResizeCommand());
            composite.Add(new BlackAndWhiteCommand());
            composite.Execute();


            var history  = new History();
            var document = new HtmlDocument {
                Content = "Hello World"
            };

            var bold = new BoldCommand(document, history);

            bold.Execute();
            Console.WriteLine(document.Content);

            var undoCommand = new UndoCommand(history);

            undoCommand.Execute();

            Console.WriteLine(document.Content);
        }