Command Design Pattern

Jack Herer 105 Reputation points
2023-04-18T15:18:59.87+00:00

Hey, can you give me an example of command design pattern?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,452 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rajkamal Gupta 225 Reputation points
    2023-04-18T15:27:08.7133333+00:00

    Here you go.. hope it's helpful :)

        internal class Program
        {
            public static void Main()
            {
                string argument = "ON";
    
                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.Open();
                }
                else if (argument == "OFF")
                {
                    // Switch (the Invoker) will invoke the Execute() on the command object.
                    _switch.Close();
                }
                else
                {
                    Console.WriteLine("Argument \"ON\" or \"OFF\" is required.");
                }
            }
        }
    
    
        /* The Invoker class */
        public class Switch
        {
            ICommand _closedCommand;
            ICommand _openedCommand;
    
            public Switch(ICommand closedCommand, ICommand openedCommand)
            {
                _closedCommand = closedCommand;
                _openedCommand = openedCommand;
            }
    
            // Close the circuit / power on
            public void Close()
            {
                _closedCommand.Execute();
            }
    
            // Open the circuit / power off
            public void Open()
            {
                _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()
            {
                Console.WriteLine("The light is on");
            }
    
            public void PowerOff()
            {
                Console.WriteLine("The light is off");
            }
        }
    
        public interface ICommand
        {
            void Execute();
        }
    
        /* The Command for turning off the device - ConcreteCommand #1 */
        public class CloseSwitchCommand : ICommand
        {
            private ISwitchable _switchable;
    
            public CloseSwitchCommand(ISwitchable switchable)
            {
                _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)
            {
                _switchable = switchable;
            }
    
            public void Execute()
            {
                _switchable.PowerOn();
            }
        }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful