private void commandPatternBtn_Click(object sender, RoutedEventArgs e) { string[] arguments = new string[1]; arguments[0] = dataInputTB.Text.Trim(); string argument = arguments.Length > 0 ? arguments[0].ToUpper() : null; ISwitchable lamp = new Light(statusBarTB); // Pass reference to the lamp instance to each command ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); // Pass reference to instances of the Command objects to the switch Switch @switch = new Switch(switchClose, switchOpen); if (argument == "ON") { // Switch (the Invoker) will invoke Execute() on the command object. @switch.Open(); } else if (argument == "OFF") { // Switch (the Invoker) will invoke the Execute() on the command object. @switch.Close(); } else { statusBarTB.Text = "Argument \"ON\" or \"OFF\" is required."; } }
public void TestMethodCommand_Example() { var d = new Random((int)DateTime.Now.Ticks); for (var i = 0; i < 5; i++) { string arg = (d.Next(1, 100)) % 2 == 0 ? "ON" : "OFF"; ISwitchable lamp = new Light(); ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); Switch _switch = new Switch(switchClose, switchOpen); if (arg == "ON") { _switch.Close(); } else if (arg == "OFF") { _switch.Open(); } else { Console.WriteLine("Argument \"ON\" or \"OFF\" is required."); } } }
public static Switch CreateSwitch(ISwitchable switchable) { var openCommand = new OpenSwitchCommand(switchable); var closeCommand = new CloseSwitchCommand(switchable); var returnSwitch = new Switch(openCommand, closeCommand); return(returnSwitch); }
static void Main(string[] args) { ISwitchable lamp = new Light(); ICommand openSwitchCommand = new OpenSwitchCommand(lamp); ICommand closeSwitchCommand = new CloseSwitchCommand(lamp); SwitchInvoker switchInvoker = new SwitchInvoker(openSwitchCommand, closeSwitchCommand); switchInvoker.Open(); switchInvoker.Close(); }
public void TestCommand() { var lamp = new Light(); var closeCommand = new CloseSwitchCommand(lamp); var openCommand = new OpenSwitchCommand(lamp); var @switch = new Switch(closeCommand, openCommand); @switch.Open(); // OUTPUT: The light is on. @switch.Close(); // OUTPUT: The light is off. }
private static void TestingByState(bool isOn) { ISwitchable lamp = new Light(); ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); var @switch = new Switch(switchClose, switchOpen); if (isOn) { @switch.Open(); } else { @switch.Close(); } }
static void Main(string[] args) { /* * More info: https://en.wikipedia.org/wiki/Command_pattern * What problems can the Command design pattern solve? * * Coupling the invoker of a request to a particular request should be avoided. * That is, hard-wired requests should be avoided. * It should be possible to configure an object (that invokes a request) with a request. * * Implementing (hard-wiring) a request directly into a class is inflexible because * it couples the class to a particular request at compile-time, * which makes it impossible to specify a request at run-time. */ string argument = Console.ReadLine(); ISwitchable lamp = new Light(); //Pass reference to the lamp instance to each command ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); //Pass reference to instances of the Command objects to the switch Switch @switch = new Switch(switchClose, switchOpen); if (argument == "ON") { // Switch (the Invoker) will invoke Execute() on the command object. @switch.Close(); } else if (argument == "OFF") { //Switch (the Invoker) will invoke the Execute() on the command object. @switch.Open(); } else { Console.WriteLine("Argument \"ON\" or \"OFF\" is required."); } }
private static void SwitchExample() { ISwitchable lamp = new Light(); // Pass reference to the lamp instance to each command ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); // Pass reference to instances of the Command objects to the switch var @switch = new Switch(); Console.Write("Please enter the number of commands: "); var numberOfCommands = int.Parse(Console.ReadLine()); for (int i = 0; i < numberOfCommands; i++) { Console.Write("Please enter command (OPEN or CLOSE): "); var arg = Console.ReadLine(); // OPEN or CLOSE if (arg == "OPEN") { // Switch (the Invoker) will invoke Execute() (the Command) on the command object @switch.StoreAndExecute(switchOpen); } else if (arg == "CLOSE") { // Switch (the Invoker) will invoke the Execute() (the Command) on the command object @switch.StoreAndExecute(switchClose); } else { Console.WriteLine("Argument \"OPEN\" or \"CLOSE\" is required."); } } Console.WriteLine("Undoing commands..."); foreach (ICommand command in @switch.History) { command.UnExecute(); } }
public static void Main() { string argument = arguments.Length > 0 ? arguments[0].ToUpper() : null; ISwitchable lamp = new Light(); // Pass reference to the lamp instance to each command ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); // Pass reference to instances of the Command objects to the switch var @switch = new Switch(switchClose, switchOpen); // Switch (the Invoker) will invoke Execute() on the command object. if (argument == "ON") { @switch.Open(); } else if (argument == "OFF") { @switch.Close(); } else { WriteLine("Argument \"ON\" or \"OFF\" is required."); } }
static void Main(string[] args) { string arg = args.Length > 0 ? args[0].ToUpper() : null; ISwitchable lamp = new Light(); ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); Switch @switch = new Switch(switchClose, switchOpen); if (arg == "ON") { @switch.Close(); } else if (arg == "OFF") { @switch.Open(); } else { Console.WriteLine("Argumento \"ON\" ou \"OFF\" é necessário."); } }
static void Main(string[] args) { if (exeCommandPattern) { string argument = args.Length > 0 ? args[0].ToUpper() : null; ISwitchable lamp = new Light(); // Pass reference to the lamp instance to each command ICommand switchClose = new CloseSwitchCommand(lamp); ICommand switchOpen = new OpenSwitchCommand(lamp); // Pass reference to instances of the Command objects to the switch Switch @switch = new Switch(switchClose, switchOpen); if (argument == "ON" || argument == "on") { // Switch (the Invoker) will invoke Execute() on the command object. @switch.Open(); } else if (argument == "OFF" || argument == "off") { // Switch (the Invoker) will invoke the Execute() on the command object. @switch.Close(); } else { Console.WriteLine("Argument \"ON\" or \"OFF\" is required."); } } if (exeInterpretPattern) { var context = new Context(); // Usually a tree var expression = new List <AbstractExpression>(); // Populate 'abstract syntax tree' expression.Add(new TerminalExpression()); expression.Add(new NonterminalExpression()); expression.Add(new TerminalExpression()); expression.Add(new TerminalExpression()); // Interpret foreach (AbstractExpression exp in expression) { exp.Interpret(context); } } if (exeCompositePattern) { // composite pattern var compositeGraphic = new CompositeGraphic(); var compositeGraphic1 = new CompositeGraphic(); var compositeGraphic2 = new CompositeGraphic(); compositeGraphic1.Add(new Ellipse()); compositeGraphic2.AddRange(new Ellipse(), new Ellipse()); compositeGraphic.AddRange( new Ellipse(), compositeGraphic1, compositeGraphic2); compositeGraphic.Print(); Console.ReadLine(); } if (exeCompositeTerminalSymbol) { var bnfExpression = new CompositeTerminalSymbol("expression"); var bnfPlus = new CompositeTerminalSymbol("plus"); var bnfMinus = new CompositeTerminalSymbol("minus"); var bnfVariable = new CompositeTerminalSymbol("variable"); var bnfDigit = new CompositeTerminalSymbol("digit"); var bnfNumber = new CompositeTerminalSymbol("number"); bnfExpression.AddRange( new TerminalSymbol("plus"), new TerminalSymbol("minus"), new TerminalSymbol("variable"), new TerminalSymbol("number")); bnfPlus.AddRange(bnfExpression, bnfExpression, new TerminalSymbol(" + ")); bnfMinus.AddRange(bnfExpression, bnfExpression, new TerminalSymbol(" - ")); bnfVariable.AddRange( new TerminalSymbol("a"), new TerminalSymbol("b"), new TerminalSymbol("c"), new TerminalSymbol("d"), new TerminalSymbol("e"), new TerminalSymbol("f"), new TerminalSymbol("g") ); bnfDigit.AddRange( new TerminalSymbol("0"), new TerminalSymbol("1"), new TerminalSymbol("2"), new TerminalSymbol("3"), new TerminalSymbol("4"), new TerminalSymbol("5"), new TerminalSymbol("6"), new TerminalSymbol("7"), new TerminalSymbol("8"), new TerminalSymbol("9") ); bnfNumber.AddRange(bnfDigit, bnfDigit, bnfNumber); bnfExpression.Print(String.Empty); bnfPlus.Print(String.Empty); bnfMinus.Print(String.Empty); bnfVariable.Print(String.Empty); bnfDigit.Print(String.Empty); //bnfNumber.Print(string.Empty); Console.ReadLine(); } }