一步步提升程序的抽象程序,努力做到相同的代码从来不写两次
相同功能的代码不写两次
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace CSLearn
{
public class CreateMenu
{
public MenuStrip GetMenu()
{
MenuStrip ms = new MenuStrip();
ToolStripMenuItem tsmi = new ToolStripMenuItem("file");
//------------------------------------------
//ver1
// tsmi.Click += new EventHandler(tsmi_Click);
//------------------------------------------
//ver2
//MyCommandDeal test = new MyCommandDeal();
//tsmi.Click += test.DealFileCommadn;
//------------------------------------------
//ver3
string dealclassname = "CSLearn.MyCommandDeal2";
object obj = Assembly.GetExecutingAssembly().CreateInstance(dealclassname);
ICommandDeal test = obj as ICommandDeal;
if (test == null)
{
MessageBox.Show("动态创建类型失败");
throw new Exception("传入的类型参数不对,创建类型失败");
}
tsmi.Click += test.DealFileCommand;
ToolStripMenuItem tsmiedit = new ToolStripMenuItem("edit");
tsmiedit.Click += new EventHandler(tsmiedit_Click);
ms.Items.AddRange(new ToolStripItem[] { tsmi,tsmiedit});
return ms;
}
//------------------------------------------
//ver1
void tsmiedit_Click(object sender, EventArgs e)
{
MessageBox.Show("use this command to edit a file");
}
void tsmi_Click(object sender, EventArgs e)
{
MessageBox.Show("Use this command to crate a menu");
}
}
//------------------------------------------
//ver2
public class MyCommandDeal
{
public void DealFileCommadn(object sender, EventArgs e)
{
MessageBox.Show("mycommand deal deal with the file command");
}
}
//------------------------------------------
//ver3
public interface ICommandDeal
{
void DealFileCommand(object sender, EventArgs e);
}
public class MyCommandDeal2:ICommandDeal
{
ICommandDeal 成员
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace CSLearn
{
public class CreateMenu
{
public MenuStrip GetMenu()
{
MenuStrip ms = new MenuStrip();
ToolStripMenuItem tsmi = new ToolStripMenuItem("file");
//------------------------------------------
//ver1
// tsmi.Click += new EventHandler(tsmi_Click);
//------------------------------------------
//ver2
//MyCommandDeal test = new MyCommandDeal();
//tsmi.Click += test.DealFileCommadn;
//------------------------------------------
//ver3
string dealclassname = "CSLearn.MyCommandDeal2";
object obj = Assembly.GetExecutingAssembly().CreateInstance(dealclassname);
ICommandDeal test = obj as ICommandDeal;
if (test == null)
{
MessageBox.Show("动态创建类型失败");
throw new Exception("传入的类型参数不对,创建类型失败");
}
tsmi.Click += test.DealFileCommand;
ToolStripMenuItem tsmiedit = new ToolStripMenuItem("edit");
tsmiedit.Click += new EventHandler(tsmiedit_Click);
ms.Items.AddRange(new ToolStripItem[] { tsmi,tsmiedit});
return ms;
}
//------------------------------------------
//ver1
void tsmiedit_Click(object sender, EventArgs e)
{
MessageBox.Show("use this command to edit a file");
}
void tsmi_Click(object sender, EventArgs e)
{
MessageBox.Show("Use this command to crate a menu");
}
}
//------------------------------------------
//ver2
public class MyCommandDeal
{
public void DealFileCommadn(object sender, EventArgs e)
{
MessageBox.Show("mycommand deal deal with the file command");
}
}
//------------------------------------------
//ver3
public interface ICommandDeal
{
void DealFileCommand(object sender, EventArgs e);
}
public class MyCommandDeal2:ICommandDeal
{
ICommandDeal 成员
}
}