Command Pattern   Leave a comment

Kapitalny przykład wzorca Command

Zapiski programisty .net

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…

View original post 254 słowa więcej

Posted 3 Maj 2015 by marekwmsdn in Bez kategorii

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

%d bloggers like this: