Software Design Patterns : Command Pattern
So As Promissed we are Starting of with command pattern, I feel that the command pattern is simple and highly resuable,
This is also called an Action pattern, and the pattern is just that it represents an action as an object, and this pattern completely decouples the client to the inner working of the command, the command is self contained meaning it handles its own dependencies and requirements, the client is just responsible calling the command, The advanatage of this being you can build an extensible framework of actions and you would not e have to write up the complex logic of looking up a command and passing arguements into it. This implementation also allows you to do a batch command processing, Logging also become easier as the execution of a command can be used to enforce logging, so does the reversal of the command,
Let me start of with a example, Even though I could have started off with a command line utility as all others do, I would take the much harder route and acually show you a realworld example, A WYSIWIG Text Editor with Simple Formatting Options. (This is would really be a very simple editor).
So Start of with the base command structure, its just a interface with a single method which doesn’t accept any parameters.
public interface ICommand { void ApplyFormatting(); }
Because we will be working with a Text Editor Let me go ahead and setup a class which will serve as the base for the all the toggle commands, Note that to keep things simple I have oversimplified the implementation and also basically limited myself to three actions but you get the Idea. Down the line i will update this to be more proper.
public abstract class ToggleCommand : ICommand, INotifyPropertyChanged { public RichTextBox TextEditor { get; set; } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string Property) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(Property)); } public ToggleCommand(RichTextBox TextEd) { TextEditor = TextEd; TextEditor.KeyDown += (s, e) => { NotifyPropertyChanged("IsChecked"); }; TextEditor.MouseClick += (s, e) => { NotifyPropertyChanged("IsChecked"); }; } public abstract bool IsChecked { get; } public abstract void ApplyFormatting(); protected void ApplyToggle(FontStyle Style) { var condition = (Style == FontStyle.Bold) ? TextEditor.SelectionFont.Bold : (Style == FontStyle.Italic) ? TextEditor.SelectionFont.Italic : TextEditor.SelectionFont.Underline; if (condition) TextEditor.SelectionFont = new Font(TextEditor.SelectionFont.FontFamily, TextEditor.SelectionFont.Size, TextEditor.SelectionFont.Style ^ Style); else TextEditor.SelectionFont = new Font(TextEditor.SelectionFont.FontFamily, TextEditor.SelectionFont.Size, TextEditor.SelectionFont.Style | Style); } protected bool IsFormattingApplied(FontStyle Style) { if (TextEditor.SelectionLength > 0) { var condition = (Style == FontStyle.Bold) ? TextEditor.SelectionFont.Bold : (Style == FontStyle.Italic) ? TextEditor.SelectionFont.Italic : TextEditor.SelectionFont.Underline; return condition; } else { var i = TextEditor.SelectionStart; TextEditor.Select(i, 1); var condition = (Style == FontStyle.Bold) ? TextEditor.SelectionFont.Bold : (Style == FontStyle.Italic) ? TextEditor.SelectionFont.Italic : TextEditor.SelectionFont.Underline; TextEditor.Select(i, 0); return condition; } } }
As mentioned the command takes care of all the requirements and is self contained, As we are working on WYSIWIG editor the command needs a RichTextEditor to work with, and implements the ICommand Interface, I have also made this implement the INotifyPropertyChanged and introduce a property which exposes the state text at current position.
From then on its straight forward implement this class
For Bold
class BoldCommand : ToggleCommand { public BoldCommand(RichTextBox TextControl) : base(TextControl) { } public override void ApplyFormatting() { ApplyToggle(FontStyle.Bold); NotifyPropertyChanged("IsChecked"); } public override bool IsChecked { get { return IsFormattingApplied(FontStyle.Bold); } } }
For Italic
public class ItalicCommand : ToggleCommand { public ItalicCommand(RichTextBox TextControl) : base(TextControl) { } public override bool IsChecked { get { return IsFormattingApplied(System.Drawing.FontStyle.Italic); } } public override void ApplyFormatting() { ApplyToggle(System.Drawing.FontStyle.Italic); NotifyPropertyChanged("IsChecked"); } }
For Underline
public class UnderlineCommand : ToggleCommand { public UnderlineCommand(RichTextBox TextControl) : base(TextControl) { } public override bool IsChecked { get { return IsFormattingApplied(System.Drawing.FontStyle.Underline); } } public override void ApplyFormatting() { ApplyToggle(System.Drawing.FontStyle.Underline); NotifyPropertyChanged("IsChecked"); } }
To Visualize this

Due to All this the client is very simple
private void Form1_Load(object sender, EventArgs e) { actionToolStripButtonBold.Command = new BoldCommand(this.richTextBox1); actionToolStripButtonItalic.Command = new ItalicCommand(this.richTextBox1); actionToolStripButtonUnderline.Command = new UnderlineCommand(this.richTextBox1); } private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { ((ActionToolStripButton)e.ClickedItem).Command.ApplyFormatting(); }
I have attched the project file for download