How does it work and why use the Command project standard?

Asked

Viewed 97 times

2

I’ve been studying the design standards and I’ve come across the Command design standard. I still don’t quite understand how it works, correct me if I’m wrong, but from what I saw why I should encapsulate an object-shaped action? Wouldn’t it be easier to just call this action directly? And when should I use this pattern?

I don’t know if I understood it 100%, but from what I’ve seen it’s possible to make things more "palpable" with Command once it turns an action into an object. I don’t find myself using it any other way, yet I leave this issue in case someone needs one day.

1 answer

1


And if you don’t know exactly what this action is, how do you solve it? You know that an action needs to be executed, but the object is to determine what it will be, it needs some mechanism to solve this.

Before I talk about this particular design pattern I want to say that the Peter Novig already said that Patterns design only exist by language deficiency, If language was powerful enough, you wouldn’t need to use any. Of course all patterns cannot be placed in languages, but the main ones could in theory be placed, although it would violate the philosophy of language.

On the other hand project pattern is everywhere, so even if the language has a capability that avoids the need for you to create code that follows the precepts of a project pattern there will still be this pattern, only it is already in the language.

That being said, there are languages that do not need to create exactly this pattern to get the same effect.

The languages they need usually need a contract that indicates which method will be used to perform any action, then every application component that needs to execute something specific knows that it can have this generic command executed by calling this method. This can be done in all types that have implemented this method, and in some languages this is done through a class or interface inheritance.

In the implementation of this method in the object that will have the command you can put the code you want to give the intended action. In general is to call some other method that has a certain action.

This is a way that the components that call the execution do not need to know what will be executed, just know that something will be executed, and the component that has action does not need to know details of how that will be called.

A common example is to have something like this (taken from Wikipedia):

using static System.Console;

namespace CommandPattern {    
    public interface ICommand {
        void Execute();
    }
    /* The Invoker class */
    public class Switch {
        ICommand closedCommand;
        ICommand openedCommand;
        public Switch(ICommand closedCommand, ICommand openedCommand) {
            this.closedCommand = closedCommand;
            this.openedCommand = openedCommand;
        }
        // Close the circuit / power on
        public void Close() => this.closedCommand.Execute();
        // Open the circuit / power off
        public void Open() => this.openedCommand.Execute();
    }
    /* An interface that defines actions that the receiver can perform */
    public interface ISwitchable {
        void PowerOn();
        void PowerOff();
    }
    /* The Receiver class */
    public class Light : ISwitchable {
        public void PowerOn() => WriteLine("The light is on");
        public void PowerOff() => WriteLine("The light is off");
    }
    /* The Command for turning off the device - ConcreteCommand #1 */
    public class CloseSwitchCommand : ICommand {
        private ISwitchable switchable;
        public CloseSwitchCommand(ISwitchable switchable) this.switchable = switchable;
        public void Execute() => switchable.PowerOff();
    }
    /* The Command for turning on the device - ConcreteCommand #2 */
    public class OpenSwitchCommand : ICommand {
        private ISwitchable switchable;
        public OpenSwitchCommand(ISwitchable switchable) this.switchable = switchable;
        public void Execute() => switchable.PowerOn();
    }
    /* The test class or client */
    internal class Program {
        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.");
        }
    }
}

I put in the Github for future reference.

So I can run something just knowing that the object has this ability to execute a command, I don’t need to know any detail of the object, it can do whatever, be anything, just need to have the command capability through the implementation of the interface.

  • 1

    There’s only one thing I didn’t understand, how I wouldn’t know which action to perform? and please could tell me when to utilize?

  • 1

    Well, it seems to me that you are falling into the situation that we always talk about, you are taking a solution and looking for a problem to apply. This doesn’t usually work. Doesn’t the example help to understand when to use? Doesn’t it demonstrate how you don’t know what to do? It is a matter of standardizing call nomenclatures, you create an indirect because at that point the code is not yet determined specific action to be executed. If you think it’s complicated, don’t use it, do it the simple way, it’s always better. Only create complications in the code if you see a need, when you’re not seeing it you don’t need it.

  • 1

    One place that is used is in the menu actions of an application for example a text editor (a Word of life), if you want to offer the option of Undo ("undo"), you encode in the object the steps to perform the action and the steps to undo that same action.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.