Command Pattern

Command pattern allows you to encapsulate a command as an object.

Interface ICommand has only one method: Execute. In this place you can also put Undo method if you would like to have this kind of functionality.

    public interface ICommand
    {
        void Execute();
    }

I would like to have possibitity to register person for some kind of training. To do this I am creating RegisterForTrainingCommand class that implements ICommand interface.

 
    public class RegisterForTrainingCommand : ICommand
    {
        private readonly ITraining _training;
        private readonly IPerson _person;

        public RegisterForTrainingCommand(ITraining training, IPerson person)
        {
            _training = training;
            _person = person;
        }

        public void Execute()
        {
            _training.Register(_person);
        }
    }

Once I have possibility to register for training I can also create command for paying for training.

    public class PayForTrainingCommand : ICommand
    {
        private readonly ITraining _training;
        private readonly IPerson _person;
        private readonly decimal _amount;

        public PayForTrainingCommand(ITraining training, IPerson person, decimal amount)
        {
            _training = training;
            _person = person;
            _amount = amount;
        }

        public void Execute()
        {
            _training.Pay(_person, _amount);
        }
    }

Now, I am creating Invoker class which is responsibility for executing all passing commands.

    public class Invoker
    {
        private readonly List _commands;

        public Invoker(List commands)
        {
            _commands = commands;
        }

        public void ExecuteCommands()
        {
            _commands.ForEach(x=>x.Execute());
        }
    }

We need an implementation of Traning class as well as Person class

    public class Training : ITraining
    {
        public string Name { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal Price { get; set; }

        public void Register(IPerson person)
        {
            Console.WriteLine("{0} {1} has been registered for training {2}", person.FirstName, person.LastName, Name);
        }

        public void Pay(IPerson person, decimal amount)
        {
            Console.WriteLine("{0} {1} has been payed {2} for training {3}", person.FirstName, person.LastName, amount, Name);
        }
    }
public class Person : IPerson
    {
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
    }

The main application looks like this

    class Program
    {
        static void Main(string[] args)
        {
            var vsTraining = new Training { Name = "Visual Studio 2012" };
            var sqlTraining = new Training {Name = "Microsoft SQL 2012"};

            var personKowalski = new Person {Id = 1, FirstName = "Jan", LastName = "Kowalski"};
            var personNowak = new Person {Id = 2, FirstName = "Krzysztof", LastName = "Nowak"};

            var commands = new List
            {                   
                new RegisterForTrainingCommand(vsTraining, personKowalski),
                new RegisterForTrainingCommand(sqlTraining, personNowak),
                new PayForTrainingCommand(sqlTraining, personNowak, 2200.99M)
            };

            var invoker = new Invoker(commands);
            invoker.ExecuteCommands();
        }
    }
Advertisements

One thought on “Command Pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s