switch case and efficiency

  • Thread starter Thread starter Martijn Mulder
  • Start date Start date
Yes, you can. Any time you would actually want to implement the command
pattern, however, you'll almost assuredly have more complex command
classes than the ones I demonstrated, especially if you're going to
support undo, so using a delegate, if even possible, would only serve to
fracture your code. The best bet is to put all the functionality into the
command class.


//When using the Command Pattern, is it OK to store the ID and the command
to invoke in the MenuItem, as in the example below? Please comment

//using...
using System;
using System.Collections.Generic;
using System.Windows.Forms;

//namespace CommandPattern
namespace CommandPattern
{

//interface ICommand
interface ICommand
{

//method Execute
void Execute();
}

//class MyToolStripMenuItem
class MyToolStripMenuItem: ToolStripMenuItem, ICommand
{

//delegate Command
public delegate void Command();

//data command
Command command;

//data id
MainForm.ID id;

//constructor
public MyToolStripMenuItem
(
string a,
MainForm.ID b,
Command c
)
{
Text=a;
id=b;
command=c;
}

//property ID
public MainForm.ID ID
{
get{return id;}
}

//method Execute
public void Execute()
{
command();
}
}

//class MainForm
class MainForm: Form
{

//enum ID
public enum ID
{
FILE,
FILE_NEW,
FILE_PRINT,
FILE_EXIT,
};

//data _mytoolstripmenuitems
Dictionary<ID, MyToolStripMenuItem> _mytoolstripmenuitems=new
Dictionary<ID, MyToolStripMenuItem>();

//data _keysToCommandsXref
Dictionary<Keys, ID> _keysToCommandsXref=new
Dictionary<Keys, ID>();

//data menustrip
MenuStrip menustrip= new MenuStrip();

//data _file, _file_new, _file_print, _file_exit
MyToolStripMenuItem _file;
MyToolStripMenuItem _file_new;
MyToolStripMenuItem _file_print;
MyToolStripMenuItem _file_exit;

//constructor
public MainForm()
{

// Set caption
Text= "The Command Pattern";

// Construct MyToolStripMenuItem _file
_file=new MyToolStripMenuItem
(
"&File",
ID.FILE,
null
);

// Construct MyToolStripMenuItem _file_new
_file_new=new MyToolStripMenuItem
(
"&New",
ID.FILE_NEW,
New
);

// Construct MyToolStripMenuItem _file_print
_file_print=new MyToolStripMenuItem
(
"&Print",
ID.FILE_PRINT,
Print
);

// Construct MyToolStripMenuItem _file_exit
_file_exit=new MyToolStripMenuItem
(
"E&xit",
ID.FILE_EXIT,
ExitApplication
);

// Make menustrip
Controls.Add(menustrip);
menustrip.Items.Add(_file);

_file.DropDown.Items.Add(_file_new);
_file.DropDown.Items.Add(_file_print);
_file.DropDown.Items.Add(_file_exit);

_file_new.Click += OnMenuItemClick;
_file_print.Click += OnMenuItemClick;
_file_exit.Click += OnMenuItemClick;

// Fill _mytoolstripmenuitems
_mytoolstripmenuitems.Add(_file_new.ID,_file_new);
_mytoolstripmenuitems.Add(_file_print.ID,_file_print);
_mytoolstripmenuitems.Add(_file_exit.ID,_file_exit);

// Fill _keysToCommandsXref
_keysToCommandsXref.Add
(
Keys.Control|
Keys.N,
_file_new.ID
);

// Fill _keysToCommandsXref
_keysToCommandsXref.Add
(
Keys.Control|
Keys.P,
_file_print.ID
);

// Fill _keysToCommandsXref
_keysToCommandsXref.Add
(
Keys.Escape,
_file_exit.ID
);
}

//methods ExitApplication, New, Print
void ExitApplication() {Application.Exit();}
void New() {MessageBox.Show("New");}
void Print() {MessageBox.Show("Print");}

//method OnMenuItemClick
void OnMenuItemClick(object sender, EventArgs e)
{
try
{
_mytoolstripmenuitems[(ID)((MyToolStripMenuItem)sender).ID].Execute();
}
catch
{
// Oops!
}
}

//method OnKeyUp
protected override void OnKeyUp(KeyEventArgs e)
{
try
{
_mytoolstripmenuitems[_keysToCommandsXref[e.KeyData]].Execute();
}
catch
{
base.OnKeyUp(e);
}
}

//method Main
static void Main()
{
Application.Run(new MainForm());
}
}
}
 
Back
Top