সোমবার, ১৮ নভেম্বর, ২০১৩

Command Pattern



  • Data driven design pattern  
  • Behavioral pattern category Gi GKwU pattern
  • GKwU action ‡K object AvKv‡i represent K‡i A_©vr Command Pattern G GKwU request ‡K object G wrap Kiv nq Ges invoker object G pass Kiv nq| 
  • Then Invoker   object †K command handle I corresponding object G pass Kivi g‡Zv appropriate object G Dnv pass K‡i| 
  • object command execute K‡i|
 Intent:
  • Command Pattern GKwU execution request ‡K object Gi g‡a¨ encapsulate K‡i|
  • Bnv request mg~‡ni store, search I transport possible K‡i Ges request mg~n‡K common interface Gi gva¨‡g call allow K‡i|
  • GB pattern ‡ÿÎwe‡k‡l undo/ macro operation implement Ki‡Z use nq|
  • Command Gi parameterized execution method _v‡K hvnv associated operation invoke Ki‡e|
  • Properties setting Gi gva¨‡g client context provide Ki‡Z cvi‡e|
UML Diagram:
wb‡¤œ Command Pattern Gi UML Diagram ‡`qv n‡jv-


Non Software Example:
Ø  Restaurnat G Dinner Gi ‡ÿ‡Î
o   Waiter or waitress customer  Gi KvQ †_‡K order/command ‡bq
o   Then order/command ‡K cook Gi Kv‡Q send Kiv nq
o   Then order/command Abyhvqx dinner prepare K‡i cook Waiter or waitress Kv‡Q send K‡i|
o   Waiter or waitress Then dinner customer  ‡K cwi‡ekb K‡i-
Ø  G‡ÿ‡Î customer  order n‡”Q command
Ø  Waiter or waitress n‡”Q Invoker
Ø  Cook n‡jv appropriate object ‡hLv‡b command execute nq|
Ø  Then executed command Invoker Gi gva¨‡g (Waiter or waitress Gi gva¨‡g) Client/Customer Gi Kv‡Q wd‡i Av‡m|

Code Example
·         GB example G Avgiv simple vending machine application build Ki‡ev hvnv‡Z cold beverages, hot coffee I peanuts _vK‡e|
·         Machine ‡_‡K cb¨  ‡bqvi Rb¨ Avgiv command use Ki‡ev|
·         cÖwZwU command Gi Rb¨ Avgiv ICommand interface implement Ki‡ev|
·         GQvov Av‡iv GKwU interface define Kiv n‡q‡Q ICommandFactory bvg| Bnv‡Z Create() bv‡g GKwU method signature define Kiv n‡q‡Q|
Ø  cÖ_‡g GKwU interface create Kwi hvi bvg ICommand
Ø  Then Dnv‡K implement K‡i GKwU class create Kwi hvi bvg GetBeverage Ges Dnv wb¤œiƒc-
using System;
using System.Diagnostics;
 
namespace CommandPatternConsole
{
    public class GetBeverageCommand : ICommand, ICommandFactory
    {
        public bool Sparkling { get; set; }
 
        #region ICommand Members
 
        public string Name
        {
            get { return "GetBeverage"; }
        }
 
        public string Description
        {
            get { return "GetBeverage [sparkling]"; }
        }
 
        public void Execute()
        {
            // Get some coffee
            Console.WriteLine(
                "Here's your drink {0} ",
                Sparkling ? "and it sparkles." : string.Empty);
 
            // Log the command
            Debug.WriteLine("{0}: {1} called",
                            DateTime.Now,
                            ToString());
        }
 
        #endregion
 
        #region ICommandFactory Members
 
        public ICommand Create(string[] args)
        {
            string arg1 = string.Empty;
 
            if (args.Length == 2)
            {
                arg1 = args[1];
            }
 
            return new GetBeverageCommand
                       {
                           Sparkling = arg1 == "sparkling",
                       };
        }
 
        #endregion
    }
}
Ø  GKwU CommandParser define Ki‡ev hvi gva¨‡g command line argument interpret Ki‡ev|
Ø  hw` right command cvq Z‡e Create() method ‡K arguments mn calling Gi gva¨‡g GKwU command execute Kiv nq|
using System.Collections.Generic;
using System.Linq;
 
namespace CommandPatternConsole
{
    public class CommandParser
    {
        private readonly IEnumerable<ICommand> _commands;
 
        public CommandParser(IEnumerable<ICommand> commands)
        {
            _commands = commands;
        }
 
        internal ICommand Parse(string[] args)
        {
            string commandName = args[0];
            ICommand command = Find(commandName);
 
            return ((ICommandFactory) command).Create(args);
        }
 
        private ICommand Find(string commandName)
        {
            return _commands
                .FirstOrDefault(c => c.Name == commandName);
        }
    }
}
using System;
using System.Collections.Generic;
 
namespace CommandPatternConsole
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            IEnumerable<ICommand> commands = GetCommands();
 
            try
            {
                var parser = new CommandParser(commands);
                ICommand command = parser.Parse(args);
                command.Execute();
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid command {0}", args);
                PrintInstructions(commands);
            }
        }
 
        private static IEnumerable<ICommand> GetCommands()
        {
            return new List<ICommand>
                       {
                           new GetBeverageCommand(),
                           new GetCoffeeCommand(),
                           new GetPeanutsCommand(),
                       };
        }
 
        private static void PrintInstructions(IEnumerable<ICommand> commands)
        {
            Console.WriteLine();
            Console.WriteLine("*** VENDING MACHINE commands ***");
            Console.WriteLine();
 
            foreach (ICommand command in commands)
            {
                Console.WriteLine("- {0}", command.Description);
            }
 
            Console.WriteLine();
        }
    }
}







কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন