Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Command design pattern
//Command design pattern //1.decouple client that executes the command from the command logic and its dependencies //2.command-line application //3.validation //4.undo using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //string[] a = new string[]{"UpdateQuantityCommand", "23"}; Run(); } public static void Run() { string[] args =new string[]{"CreateOrder", "24"}; var availableCommands = GetAvailableCommands(); if (args.Length == 0) { PrintUsage(availableCommands); return; } var parser = new CommandParser(availableCommands); var command = parser.ParseCommand(args); if(null != command) command.Execute(); else {Console.WriteLine("Error, available commands: "); PrintUsage(availableCommands); } } private static void PrintUsage(IEnumerable<ICommandFactory> availableCommands) { foreach (var command in availableCommands) Console.WriteLine(command.Description); } static IEnumerable<ICommandFactory> GetAvailableCommands() { return new ICommandFactory[] { new CreateOrderCommand(), new UpdateQuantityCommand() //new ShipOrderCommand(), //new MojCommand() }; } public class CommandParser { readonly IEnumerable<ICommandFactory> availableCommands; public CommandParser(IEnumerable<ICommandFactory> availableCommands) { this.availableCommands = availableCommands; } public ICommand ParseCommand(string[] args) { var requestedCommandName = args[0]; var command = availableCommands.FirstOrDefault(c=>c.CommandName == requestedCommandName); if (command != null) return command.MakeCommand(args); return null; } } public interface ICommand { void Execute(); } public interface ICommandFactory { string CommandName{get; } string Description{get; } ICommand MakeCommand(string[] arguments); } public class UpdateQuantityCommand : ICommand, ICommandFactory { public int NewQuantity{get;set;} public void Execute() { const int oldQ = 5; Console.WriteLine("Database updated"); Console.WriteLine("LOG: Updated order quantity form {0} to {1}", oldQ, NewQuantity); } public string CommandName {get {return "UpdateQuantity";}} public string Description {get {return "UpdateQuantity number";}} public ICommand MakeCommand(string[] arguments) { return new UpdateQuantityCommand{NewQuantity = int.Parse(arguments[1])}; } } public class CreateOrderCommand : ICommand, ICommandFactory { public int OrderNo{get;set;} public void Execute() { const int oldQ = 5; Console.WriteLine("Database updated"); Console.WriteLine("LOG: Created order No {0}", OrderNo); } public string CommandName {get {return "CreateOrder";}} public string Description {get {return "CreateOrder number";}} public ICommand MakeCommand(string[] arguments) { return new CreateOrderCommand{OrderNo = int.Parse(arguments[1])}; } } } }
run
|
edit
|
history
|
help
0
tool
d
gfhfgsdfsfdsdghgfddfg
Explicit Interface Examplle
EnumToList
SortedDictionary<TKey, TValue> クラス
C# Calculator Simple
PermuteAString
Main4-5
dgh ret ertfh fhf hft85487