一个简单的命令模式类

using System;
using System.IO;
using EPS.UI.CommonBase;
using System.Collections;
using System.Reflection;
using System.Windows.Forms;
using Sunisoft.IrisSkin;
using DevExpress.XtraNavBar;

namespace EPS.UI.MainFrame
{
 /// <summary>
 /// Command 命令模式抽象类
 /// </summary>
    public abstract class Command
 {
  protected string _resourceName;

  // 克隆方法
  public abstract Command Clone();
  // 执行方法
  public abstract bool Execute();
  // 撤销方法
  public abstract bool Undo();
  // 重做方法
  public abstract bool Redo();

  //该命令的名字
  public string ResourceName
  {
   get { return _resourceName;}
  }

  public abstract string Description
  {
   get;
  }
 }

 /// <summary>
 /// 加载外部Form的命令
 /// </summary>
 public class FormNodeClickCommand : Command
 { 
  //全局FormHash表
        public static System.Collections.Hashtable g_FormTable = Hashtable.Synchronized(new Hashtable());
  
  //文件名
  protected string _resourceFileName;
  //当前主窗体指针
  protected Form _mainFrame = null;

  public FormNodeClickCommand(Form mainFrame, string fileName, string itemTitle)
  {
   this._mainFrame = mainFrame;
   this._resourceFileName = fileName;
   this._resourceName = itemTitle;
  }

  private void InitialForm()
  {
   string appPath = System.Environment.CurrentDirectory;
   string filePath = appPath + @"/" + this._resourceFileName;

   Assembly formAssembly = Assembly.LoadFrom(filePath);

   Type frmType = formAssembly.GetType("EPS.UI.SecurityManager.SecurityManager");
   Object frmObject = Activator.CreateInstance(frmType);
   FormBase loadform = (FormBase)frmObject;
   
   loadform.MdiParent = this._mainFrame;
   loadform.Show();
   //加入到全局表中
   g_FormTable.Add(_resourceName, loadform);   
  }

  public override bool Execute()
  {
   FormBase form = (FormBase)g_FormTable[_resourceName];

   if (form == null)
   {
    //重新加载
    InitialForm();
    return true;
   }
   else
   {
    if (form.IsDisposed)
    {
     //移除
     g_FormTable.Remove(_resourceName);
     //重新加载
     InitialForm();
     return true;
    }
    else
    {
     //重置到前面页
     form.MdiParent = this._mainFrame;
     form.Activate();
     form.WindowState = FormWindowState.Maximized;
     return true;
    }
   }   
  }

  public override bool Redo()
  {
   return false;
  }

  public override bool Undo()
  {
   return false;
  }

  public override string Description
  {
   get
   {
    return this._resourceName;
   }
  }

  public override Command Clone()
  {
   return new FormNodeClickCommand(this._mainFrame, this._resourceFileName, this._resourceName);
  }

 }

}

你可能感兴趣的:(String,object,command,assembly,null,Class)