Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Command Pattern

Posted in Design Patterns
This post has been read 3936 times

In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Command Pattern. You can read about rest of the patterns from the following links

  1. Strategy Pattern
  2. State Pattern
  3. Template Method Pattern
  4. Chain of Responsibility Pattern

You can read about the Structural patterns from here.
You can read about the Creational patterns from here.

The Command pattern deals with segregation of the client that requested an operation and the object that can perform that operation. The command pattern can support sending requests to different receivers, queuing of requests, redo and undo functionality.

    1 namespace PatternsConsole

    2 {

    3     public class CommandPattern

    4     {

    5         // Command

    6         interface ICommand

    7         {

    8             void Execute();

    9         }

   10 

   11         // ConcreteCommand

   12         class AddCommand : ICommand

   13         {

   14             public Item Itm { get; set; }

   15             public Category Cat { get; set; }

   16 

   17             public void Execute()

   18             {

   19                 Itm.Add( Cat );

   20                 Cat.Add( Itm );

   21             }

   22         }

   23 

   24         // ConcreteCommand

   25         class DeleteCommand : ICommand

   26         {

   27             public Item Itm { get; set; }

   28             public Category Cat { get; set; }

   29 

   30             public void Execute()

   31             {

   32                 Itm.Delete( Cat );

   33                 Cat.Delete( Itm );

   34             }

   35         }

   36 

   37         // Invoker

   38         class ItemManager

   39         {

   40             public ICommand Command { set; get; }

   41 

   42             public void Process()

   43             {

   44                 Command.Execute();

   45             }

   46         }

   47 

   48         // Receiver

   49         class Item

   50         {

   51 

   52             private IDictionary<string, Category> categories;

   53 

   54             public string Desc { get; set; }

   55 

   56             public Item( string itemName )

   57             {

   58                 Desc = itemName;

   59                 categories = new Dictionary<string, Category>();

   60             }

   61 

   62             public void Add( Category cat )

   63             {

   64                 categories.Add( cat.Desc, cat );

   65             }

   66 

   67             public void Delete( Category cat )

   68             {

   69                 categories.Remove( cat.Desc );

   70             }

   71 

   72         }

   73 

   74         // Receiver

   75         class Category

   76         {

   77 

   78             private IDictionary<string, Item> items;

   79 

   80             public string Desc { get; set; }

   81 

   82             public Category( string catName )

   83             {

   84                 Desc = catName;

   85                 items = new Dictionary<string, Item>();

   86             }

   87 

   88             public void Add( Item item )

   89             {

   90                 items.Add( item.Desc, item );

   91                 Console.WriteLine( "Item '" + item.Desc + "' has been added to the '"

   92                     + Desc + "' Category " );

   93             }

   94 

   95             public void Delete( Item item )

   96             {

   97                 items.Remove( item.Desc );

   98                 Console.WriteLine( "Item '" + item.Desc

   99                     + "' has been deleted from the '" + Desc + "' Category " );

  100             }

  101         }

  102 

  103         public static void Main()

  104         {

  105             // Create Receiver objects

  106             Item DVD = new Item( "The Fountain" );

  107             Category catDVD = new Category( "DVD" );

  108 

  109             // create the command object

  110             ICommand command = new AddCommand { Itm = DVD, Cat = catDVD };

  111 

  112             // create the invoker

  113             ItemManager manager = new ItemManager { Command = command };

  114             manager.Process();

  115 

  116             // Add an item to the CD category

  117             DVD = new Item( "The Wrestler" );

  118             catDVD = new Category( "DVD" );

  119             command = new AddCommand { Itm = DVD, Cat = catDVD };

  120             manager.Command = command;

  121             manager.Process();

  122 

  123             // Add an item to the Old Releases category

  124             DVD = new Item( "Pi" );

  125             catDVD = new Category( "Old Releases" );

  126             command = new AddCommand { Itm = DVD, Cat = catDVD };

  127             manager.Command = command;

  128             manager.Process();

  129 

  130             // Delete an item from the Old Releases category

  131             command = new DeleteCommand { Itm = DVD, Cat = catDVD };

  132             manager.Command = command;

  133             manager.Process();

  134 

  135             // Output:

  136             // Item 'The Fountain' has been added to the 'DVD' Category

  137             // Item 'The Wrestler' has been added to the 'DVD' Category

  138             // Item 'Pi' has been added to the 'Old Releases' Category

  139             // Item 'Pi' has been deleted from the 'Old Releases' Category

  140 

  141             Console.ReadLine();

  142         }

  143     }

  144 }

We can use the command pattern when we have different receivers and handle each in a different way or we want to specify and execute commands at different times or when we want to support undo functionality.

In my next post I will be writing about the Iterator pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll