Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

Behavioral Patterns: Implementing the Command Pattern in C#

Rate me:
Please Sign up or sign in to vote.
3.38/5 (16 votes)
23 Aug 2007CPOL2 min read 67.9K   478   58   12
Writing the Command Pattern with C# (real example).

Introduction

This article shows how to implement the behavioral Command Pattern with C# 2.0 and .NET in a real case.

What's a Design Pattern?

A Design Pattern is a general repeatable solution to a commonly occurring problem in software design. A Design Pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

What's a Behavioral Design Pattern

Behavioral Design Patterns are Design Patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out the communication.

Purpose

The Command Pattern is a Design Pattern in which objects are used to represent actions. A command object encapsulates an action and its parameters.

Structure

  • Command, declares an interface for executing an operation.
  • Concrete Command, defines a binding between a Receiver object and an action. Implements Execute by invoking the corresponding operation(s) on the Receiver.
  • Client, creates a Concrete Command object and sets its receiver.
  • Invoker, asks the Command to carry out the request.
  • Receiver, knows how to perform the operations associated with carrying out the request.

Real case

This example shows how to fill different controls with the Command Pattern. The source for each control is the file of Custom Resources.

Note: each control implements a different binding logic, but across the Command Pattern, this is hidden to client.

Let's write the code for the Command

  1. Write the Command.
  2. This interface contains the method to call for executing the Receiver code:

    C#
    // Command
    public interface ICommand    
    {
        void Execute();
    }
  3. Write the Concrete Command.
  4. This class defines a binding between the Receiver and the Execute Command action:

    C#
    // Concrete Command
    public class ConcreteCommand: ICommand
    {
        private IFiller _filler;
    
        public ConcreteCommand(IFiller filler)
        {
            _filler = filler;
        }
    
        public void Execute()
        {
            _filler.Fill();
        }
    }
  5. Write the "Common Receiver".
  6. This interface contains the common method for all controls to bind. This interface provides a common aspect for all the controls that want to use the Command.

    C#
    public interface IFiller
    {
        void Fill();
    }
  7. Write the "Invoker".
  8. This class allows to call the Execute method for each item that implements IFiller.

    C#
    // Invoker
    public class Invoker
    {
        private IFiller[] _filler;
    
        public Invoker(IFiller[] filler)
        {
            _filler = filler;
        }
    
        public void Execute()
        {
            ICommand command;
            foreach (IFiller item in _filler)
            {
                command = new ConcreteCommand(item);
                command.Execute();
            }
    
        }
    }

Let's write the code for the Client

  1. Write the Receiver(s).
  2. The following custom controls (CustomLabel, CustomListBox, CustomListView) are controls that derive from the Label, ListBox, and ListView controls.

    Thes controls implement the "Common Receiver" interface for implementing the binding logic:

    C#
    // Receiver
    public class CustomLabel: Label, IFiller
    {
        public void Fill()
        {
            this.Text = Resources.ResourceManager.GetString(this.Name);
        }
    }
    
    // Receiver
    public class CustomListBox: ListBox, IFiller
    {
        public void Fill()
        {
            this.Items.Clear();
            int index=1;
            string item = Resources.ResourceManager.GetString(
                string.Format("{0}{1}{2}", 
                this.Name, 
                "_", 
                index.ToString()));
            while (item != null)
            {
                this.Items.Add(item);
                index++;
                item = Resources.ResourceManager.GetString(
                string.Format("{0}{1}{2}",
                this.Name,
                "_",
                index.ToString()));
            }
        }
    }
    
    // Receiver
    public class CustomListView: ListView, IFiller
    {
        public void Fill()
        {
            this.Columns.Clear();
            int index = 1;
            string item = Resources.ResourceManager.GetString(
                 string.Format("{0}{1}{2}",
                 this.Name,
                 "_",
                 index.ToString()));
            while (item != null)
            {
                this.Columns.Add(item);
                index++;
                item = Resources.ResourceManager.GetString(
                string.Format("{0}{1}{2}",
                this.Name,
                "_",
                index.ToString()));
            }
        }
    }
  3. Write the Client.
  4. The Client calls the Invoker that invokes the Command.

    After inserting the custom controls in a form, I wrote recursive functions to find controls that implement IFiller, and then I call the Invoker on the List of IFiller controls.

    C#
    public partial class Form1 : Form
    {
        private List<ifiller> _filler;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            _filler = new List<ifiller />();
            searchControl(this.Controls);
            
            // Client
            Invoker invoker = new Invoker(_filler.ToArray());
            invoker.Execute();
        }
    
        private void searchControl(Control.ControlCollection ctrCollection)
        {
            foreach (Control ctr in ctrCollection)
            {
                IFiller fctr = ctr as IFiller;
                if (fctr != null)
                    _filler.Add(fctr);
                if (ctr.Controls.Count > 0)
                    searchControl(ctr.Controls);
            }
        }
    }

History

This is the first iteration of this code. Please provide any feedback as to whether you have used this or not, or on any problems that anyone has found with it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
kirubaraja12-Sep-12 0:18
kirubaraja12-Sep-12 0:18 
GeneralMy vote of 3 Pin
0x49D118-Apr-11 21:15
0x49D118-Apr-11 21:15 
QuestionWhat problem do your code solve? Pin
jgauffin26-Aug-08 8:54
jgauffin26-Aug-08 8:54 
GeneralInteresting Idea Pin
merlin98124-Aug-07 4:27
professionalmerlin98124-Aug-07 4:27 
QuestionInvoker? Pin
dejavudesi23-Aug-07 3:50
dejavudesi23-Aug-07 3:50 
AnswerRe: Invoker? Pin
Francesco Carata23-Aug-07 20:18
Francesco Carata23-Aug-07 20:18 
QuestionSo what is Command Pattern? Pin
Zhi Chen13-Jul-07 6:23
Zhi Chen13-Jul-07 6:23 
AnswerRe: So what is Command Pattern? Pin
Francesco Carata14-Jul-07 6:26
Francesco Carata14-Jul-07 6:26 
AnswerRe: So what is Command Pattern? Pin
endian6751-Aug-07 22:51
endian6751-Aug-07 22:51 
GeneralRe: So what is Command Pattern? Pin
Francesco Carata1-Aug-07 22:56
Francesco Carata1-Aug-07 22:56 
Generalmultiple casts Pin
Meile10-Jul-07 1:14
Meile10-Jul-07 1:14 
GeneralRe: multiple casts Pin
Francesco Carata10-Jul-07 1:26
Francesco Carata10-Jul-07 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.