Search Code Guru

Command Design Pattern

What is a design pattern

Design pattern is occurring solution to the problem that occurs during software design.

In this article, we are going to discuss the Command design pattern.

Decorator Design pattern

The command pattern is a behavioural design pattern. The pattern encapsulates a request as an object that contains all the information about the request. It even allows operations like undo and redo.

Let’s consider the scenario where you are developing the application and when user exit the app you want to present user to option that do you want to save,close or save and close the application. This problem is the perfect candidate for the command design pattern.

  • Create a class named Command and add one abstract method Execute as shown below’’
    csharp public abstract class Command { public abstract void Execute(); }

  • Create three sub classes of Command named ExitCommand,SaveCommand and SaveAndCloseCommand and inhrits these from Command base class.

    
    	public class SaveCommand : Command
    	{
    		public override void Execute()
    		{
    			Console.WriteLine("Your work saved!");
    		}
    
    	}
    
    public class ExitCommand : Command
    {
    	public override void Execute()
    	{
    		Console.WriteLine("Closing The Application!");
    	}
    }
    public class SaveAndCloseCommand : Command
    {
        private SaveCommand _save;
    	private ExitCommand _exit;
    	public SaveAndCloseCommand()
    	{
    		_save=new SaveCommand();
    		_exit=new ExitCommand();
    	}
    	public override void Execute()
    	{
    		_save.Execute();
    		_exit.Execute();
    		//Console.WriteLine("Save and Close");
    	}
    }```
    
    
  • Now create a class Application as shown below

	public class Application
	{
		private List<Command> _commands;
		private Command _command;
		public Application()
		{
		   _commands=new List<UserQuery.Command>();
		}
		public void SetCommand(Command command){
			_commands.Add(command);
			_command=command;
			
		}
		public void Execute()
		{
			this._command.Execute();
		}
	}
  • To use the API application in your application.
	void Main()
	{
		var response=Console.ReadLine();
		var app = new Application();
		switch (response)
		{
			case "s":
				app.SetCommand(new SaveCommand());
				break;
			case "c":
				app.SetCommand(new ExitCommand());
				break;
			case "sc":
				app.SetCommand(new SaveAndCloseCommand());
				break;
			default:
				Console.WriteLine("Invalid command");
				break;
			
					
		}
		
		
		app.Execute();
	}

Post a Comment

0 Comments